home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / mac / Mac_Files / Software Utilities / NN PreProcessing / FINPRE2.C < prev    next >
C/C++ Source or Header  |  1992-09-30  |  67KB  |  2,175 lines

  1. /* 22:00 24-Sep-92  (finpre.c)  Financial Pre-processing Driver */
  2.  
  3. #include "fin.h"    /* includes stdio.h, etc. */
  4. #include "fincmd.h"
  5. #include <time.h>
  6.  
  7. #define NLDS        /* include commands for non-linear dynamical systems */
  8. #undef    CORRDIMORG    /* do not use original correlation dim code */
  9.  
  10. /************************************************************************
  11.  * Copyright(C) 1992 High-Tech Communications.                          *
  12.  * 103 Buckskin Court, Sewickley, PA 15143                              *
  13.  *                                                                      *
  14.  * Written by Casimir C. Klimasauskas                                   *
  15.  *                                                                      *
  16.  * All rights reserved.  No part of this program may be reproduced,     *
  17.  * stored in a retrieval system, or tramsmitted, in any form or by any  *
  18.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  19.  * without the prior written permission of the copyright owner,         *
  20.  * High-Tech Communications.                                            *
  21.  *                                                                      *
  22.  * These programs are supplied on an "as-is" basis with no warranties   *
  23.  * of fitness or operability, either express or implied.                *
  24.  *                                                                      *
  25.  ************************************************************************
  26.  
  27.  20-Sep-92 cck
  28.     Added Non-Linear Dynamical Systems routines to menus
  29.  */
  30.  
  31. /************************************************************************
  32.  *                                    *
  33.  *    Financial Data Pre-processor                    *
  34.  *                                    *
  35.  ************************************************************************
  36.  */
  37.  
  38. typedef struct _field {    /* field in master data-base */
  39.     float    *FDataFP;    /* data */
  40.     char    *FTickCP;    /* ticker */
  41.     char    *FSymCP;    /* label */
  42.     short     FFlagI;    /* flags */
  43. #define    FF_DB        0x01    /*   field from data-base */
  44. #define    FF_DRV        0x02    /*   field is derived */
  45. #define    FF_INTRP    0x04    /*   field has been interpolated */
  46. } FIELD;
  47.  
  48. typedef struct _fdes {        /* field descriptor */
  49.     FIELD    *FFieldTP;    /* pointer to field */
  50.     char    *FTickCP;    /* ticker */
  51.     char    *FSymCP;    /* label */
  52.     short     FOffI;        /* offset */
  53.     short     FFlagI;    /* flags */
  54. #define    FD_TICK        0x01    /*   ticker is present */
  55. #define    FD_SYM        0x02    /*   symbol is present */
  56. #define    FD_OFF        0x04    /*   offset is present */
  57. #define    FD_FOUND    0x10    /*   field found in field table */
  58.     int     FldXI;        /* field index if found */
  59. } FDES;
  60.  
  61. /* Global storage for the data-base & transformations.  The
  62.  * date is kept in a separate array (FDateLP).  The array
  63.  * for the date is dynamically allocated when the size of the
  64.  * data-base is known.  Field vectors are dynamically allocated
  65.  * when needed.  The dictionary is allocated separately at the 
  66.  * very start of the program.  The purpose of these procedures is
  67.  * to try to keep from fragmenting memory any more than necessary.
  68.  */
  69.  
  70. FIELD     FldTA[MAXTF+1] = {0};    /* master field array */
  71. long    *FDateLP    = {0};    /* pointer to master date array */
  72. int     FieldsI    = {0};    /* # of fields in data-base */
  73. int     FVecI        = {0};    /* # of rows in data-base */
  74.  
  75. /* Command data storage.  The following items are the result of 
  76.  * parsing various commands.  The data is stored in this area
  77.  * and used by the global interface routines.
  78.  */
  79. char     XFieldCA[80]    = {"xfield"};    /* raw x-field descriptor */
  80. FDES     XFieldT    = {0};        /* parsed x-field descriptor */
  81. char     YFieldCA[80]    = {"yfield"};    /* raw y-field descriptor */
  82. FDES     YFieldT    = {0};        /* parsed y-field descriptor */
  83.  
  84. FILE    *LogFP        = {0};        /* log file */
  85. char     LogFile[120]    = {"none"};    /* logging file */
  86.  
  87. char     DBFile[120]    = {"data.db"};    /* data base file */
  88. char     DBStrDateCA[20]= {"00/00/00"};    /* start date */
  89. char     DBEndDateCA[20]= {"12/31/99"};    /* end date */
  90. long     DBStrDateL    = {0x19000000L};/* start date */
  91. long     DBEndDateL    = {0x19991231L};/* end date */
  92.  
  93. char     OutFile[120]    = {"data.nna"};    /* training file */
  94. char     OFStrDateCA[20]= {"00/00/00"};    /* start date */
  95. char     OFEndDateCA[20]= {"12/31/99"};    /* end date */
  96. long     OFStrDateL    = {0x0};    /* start date */
  97. long     OFEndDateL    = {0x19991231L};/* end date */
  98.  
  99. char     SaveFile[120]    = {"save.db"};    /* save data-base */
  100. char     SVStrDateCA[20]= {"00/00/00"};    /* start date */
  101. char     SVEndDateCA[20]= {"12/31/99"};    /* end date */
  102. long     SVStrDateL    = {0x0};    /* start date */
  103. long     SVEndDateL    = {0x19991231L};/* end date */
  104.  
  105. double     MinScaleD    = {-1.};    /* minimum scale */
  106. double     MaxScaleD    = { 1.};    /* maximum scale */
  107.  
  108. double     ExpFactorD    = {0.3};    /* exponential decay factor */
  109. int     SpanI        = {5};        /* # of days to span */
  110. int     SmoothI    = {5};        /* # of days to smooth */
  111.  
  112. /* Global data for analysis section */
  113.  
  114. double     ShortLowD    = {-1.0};    /* low limit for short position */
  115. double     ShortHighD    = {-0.2};    /* high limit for short position */
  116. double     LongLowD    = { 0.2};    /* low limit for long position */
  117. double     LongHighD    = { 1.0};    /* high limit for long position */
  118. double     CapitalD    = { 10000. };    /* starting capital */
  119. char     PrFldCP[40]    = { "" };    /* price field */
  120. float    *PriceFP    = {0};        /* price data itself */
  121.  
  122. char     NetFileCA[120]    = {"untitled.nnr"}; /* ".nnr" file for analysis */
  123. char     NetDateCA[20]  = {"00/00/00"};    /* start date for network */
  124. long     NetDateL    = {0};        /* start date for network */
  125. float    *NetValFP[10]    = {0};        /* network data */
  126. int     NetFieldsI    = {0};        /* # of fields in network */
  127.  
  128. char     AnlDateCA[20]    = {"00/00/00"};    /* analysis start date */
  129. long     AnlDateL    = {0};        /* start of analysis date */
  130. int     AnlFldXI    = {0};        /* index of field to analyze */
  131.  
  132. #ifdef NLDS
  133. char     NFieldCA[80]    = {"field"};    /* raw NLDS analysis field */
  134. FDES     NFieldT    = {0};        /* parsed field */
  135. char     NStrDateCA[20] = {"00/00/00"};    /* start date */
  136. long     NStrDateL    = {0};        /* binary start date */
  137. char     NEndDateCA[20] = {"12/31/99"};    /* end date */
  138. long     NEndDateL    = {0x19991231L};/* binary end date */
  139.  
  140. int     StartWinI    = {5};        /* start window size */
  141. int     DeltaWinI    = {5};        /* delta window size */
  142. int     IterationsI    = {50};        /* number of iterations to perform */
  143. int     DerivativeI    = {1};        /* derivative of series */
  144. int     EmbeddingI    = {3};        /* embedding dimension */
  145. int     StartDimI    = {2};        /* start dimension */
  146. int     EndDimI    = {6};        /* end dimension */
  147. int     tauI        = {1};        /* phase space shift */
  148. int     EvolveI    = {3};        /* minimum steps for evolution */
  149. int     LagMinimumI    = {10};        /* minimum lag between points */
  150. double     dtD        = {0.1};    /* change in radius */
  151. double     radiusD    = {0.05};    /* initial radius */
  152. double     NMinScaleD    = {0.1};    /* minimum value for scale */
  153. double     NMaxScaleD    = {2.5};    /* maximum value for scale */
  154.  
  155. char     PrintFileCA[80]= {"untitled.prn"}; /* printer output file */
  156. #define    PRT_TEXT    0        /*   formatted text output */
  157. #define    PRT_COMMA    1        /*   lotus style output */
  158. #define    PRT_TAB        2        /*   excel style output */
  159. int     PrintTypeI    = {PRT_TEXT};    /* mode of printed output */
  160. FILE    *PrintFileFP    = {0};        /* file pointer for output file */
  161. long     PrintFilePosL    = {0};        /* file position when opened */
  162.  
  163. char     TypeFileCA[80] = {"untitled.prn"}; /* type the print file */
  164.  
  165. int     NFieldFlagI    = {0};        /* valid field flag */
  166. float    *NFldDataFP    = {0};        /* pointer to the data */
  167. int     NFldStrXI    = {0};        /* start index */
  168. int     NFldLengthI    = {0};        /* # of items to use */
  169. #endif
  170.  
  171. /************************************************************************
  172.  *                                    *
  173.  *    ClearDBI() - Clear out a loaded Data-base            *
  174.  *                                    *
  175.  ************************************************************************
  176.  */
  177.  
  178. int ClearDBI()        /* clear out fields allocated for a data-base */
  179. {
  180.     int         wxI;    /* work index */
  181.     FIELD    *fldP;    /* field pointer */
  182.  
  183.     for( wxI = 0; wxI < ASof(FldTA); wxI++ ) {
  184.     fldP = &FldTA[wxI];
  185.     if ( fldP->FDataFP != (float *)0 ) free( (void *)fldP->FDataFP );
  186.     fldP->FDataFP = (float *)0;
  187.     fldP->FTickCP = (char *)0;
  188.     fldP->FSymCP  = (char *)0;
  189.     fldP->FFlagI  = 0;
  190.     }
  191.  
  192.     FDateLP = (long *)0;    /* NOTE: allocated as "FldTA[0].FDataFP" */
  193.     FVecI = FieldsI = 0;
  194.  
  195.     for( wxI = 0; wxI < ASof(NetValFP); wxI++ ) {
  196.     if ( NetValFP[wxI] != (float *)0 ) free( (void *)NetValFP[wxI] );
  197.     NetValFP[wxI] = (float *)0;
  198.     }
  199.     NetFieldsI = 0;
  200.     PriceFP = (float *)0;
  201.     strcpy( PrFldCP, "" );
  202.  
  203.     DCClearV();        /* Clear & re-initialize the dictionary */
  204.  
  205.     return( 0 );
  206. }
  207.  
  208. /************************************************************************
  209.  *                                    *
  210.  *    LoadDBI() - Load a Data-base                    *
  211.  *                                    *
  212.  ************************************************************************
  213.  */
  214.  
  215. int LoadDBI( fnCP, SDateL, EDateL )    /* load a data-base */
  216. char        *fnCP;        /* file name of data-base */
  217. long         SDateL;    /* start date */
  218. long         EDateL;    /* end date */
  219. {
  220.     FILE    *fp;        /* file pointer to data-base */
  221.     int         nfldI;        /* # of fields */
  222.     int         cfldI;        /* current field index */
  223.     long     nlinL;        /* # of lines */
  224.     int         ncfldI;    /* # of current fields */
  225.     long     linL;        /* length of longest line */
  226.     long     clinL;        /* length of current line */
  227.     unsigned     linU;        /* unsigned line length */
  228.     int         chI;        /* work character */
  229.     int         nhdrI;        /* # of items in the header */
  230.     int         rcI;        /* return code */
  231.     int         stateI;    /* current state */
  232.     int         rowI;        /* current row of input data */
  233.     FIELD    *fldP;        /* field pointer */
  234.     char    *DBbufCP;    /* data-base buffer */
  235.     char    *sp, *ap;    /* work character pointer */
  236.     char    *dp;        /* date pointer */
  237.     long     dL;        /* date */
  238.     char     buf[80];    /* message & work buffer */
  239.  
  240.     /* --- pre-set up --- */
  241.  
  242.     if ( SDateL == 0 && EDateL == 0 ) {
  243.     EDateL = 0x19991231L;
  244.     } else if ( SDateL > EDateL ) {
  245.     linL = SDateL; SDateL = EDateL; EDateL = linL;
  246.     }
  247.  
  248.     /* --- Open the File --- */
  249.  
  250.     rcI = 0;            /* return code */
  251.     DBbufCP = (char *)0;    /* no data-base buffer yet */
  252.     fp = (FILE *)0;        /* file is closed */
  253.     ClearDBI();            /* free any existing memory */
  254.  
  255.     if ( (fp = fopen( fnCP, "rb" )) == (FILE *)0 ) {
  256.     sprintf( buf, "Could not open data-base input file <%s>\n", fnCP );
  257.     goto Error;
  258.     }
  259.  
  260.     /* --- Count the number of fields & lines in the data-base --- */
  261.  
  262.     nfldI = ncfldI = 0;
  263.     nlinL = 1L;
  264.     linL  = clinL = 0L;
  265.     stateI = 0;
  266.     dp = &buf[0];
  267.     while( (chI = getc(fp)) != EOF ) {
  268.     clinL++;            /* current line length */
  269.     if ( chI > ' ' && stateI <= 1 ) {
  270.         stateI = 1;
  271.         if ( dp < &buf[sizeof(buf)-16] )
  272.         *dp++ = chI;
  273.     } else if ( stateI == 1 ) {
  274.         *dp = 0;
  275.         stateI = 2;
  276.     }
  277.     switch( chI ) {
  278.     case '\n':
  279.         if ( clinL > linL ) linL = clinL;    /* longest line */
  280.         clinL = 0L;
  281.  
  282.         if ( stateI == 2 ) {
  283.         dL = Date2BinL( buf );
  284.         if ( SDateL <= dL && dL <= EDateL ) {
  285.             nlinL++;
  286.             if ( ncfldI > nfldI ) nfldI = ncfldI;
  287.         }
  288.         }
  289.  
  290.         ncfldI = 0;
  291.         stateI = 0;
  292.         dp = &buf[0];
  293.         break;
  294.  
  295.     case '\t':
  296.         ncfldI++;
  297.         break;
  298.     }
  299.     }
  300.  
  301.     /* --- digest information about lines & fields --- */
  302.  
  303.     fseek( fp, 0L, 0 );        /* rewind input file */
  304.     if ( linL > 32000L ) {    /* check for maximum line size */
  305.     sprintf( buf, "Longest line is too long for program (%ld)\n", linL );
  306.     goto Error;
  307.     }
  308.     linU = linL+16;        /* allocate the input line buffer */
  309.     if ( (DBbufCP = (char *)malloc( linU )) == (char *)0 ) {
  310.     sprintf( buf, "Could not allocate line buffer size (%ld)\n", linL );
  311.     goto Error;
  312.     }
  313.  
  314.     /* --- Read in the data-base --- */
  315.  
  316.     if ( (nlinL * sizeof(float)) > MAXVECSIZE ) {
  317.     sprintf( buf, "Too much storage (%ld/%ld) required per item\n",
  318.         nlinL*sizeof(float), (long)MAXVECSIZE );
  319.     goto Error;
  320.     }
  321.     linU = nlinL * sizeof(float);
  322.     stateI = 0;
  323.     rowI   = 0;
  324.     while( (sp = fgets( DBbufCP, linU, fp )) != (char *)0 ) {
  325.     while( *sp && *sp <= ' ' ) sp++;
  326.     if ( *sp == '!' || *sp == 0 ) continue;
  327.     sp = DBbufCP;        /* reset line pointer for tab intercept */
  328.     switch( stateI ) {
  329.     case 0:            /* first line of header */
  330.         for( cfldI = 0; *sp != 0 && cfldI < ASof(FldTA); cfldI++ ) {
  331.         fldP = &FldTA[cfldI];            /* current field */
  332.         for( ap=sp; *ap && *ap != '\t'; ) ap++;    /* skip field */
  333.         chI = *ap;                /* save char */
  334.         *ap = 0;                /* null terminate */
  335.  
  336.         fldP->FTickCP = DCDupCP( DCLwrPkCP(sp) );
  337.         fldP->FFlagI  = FF_DB;            /* data-base field */
  338.         if ( (fldP->FDataFP = (float *)malloc( linU )) == (float *)0 ) {
  339.             sprintf( buf,
  340.             "Could not allocate space (%u) for vector (%d)\n",
  341.             linU, cfldI );
  342.             goto Error;
  343.         }
  344.         if ( cfldI == 0 ) FDateLP = (long *)fldP->FDataFP;
  345.  
  346.         if ( chI == '\t' ) ap++;        /* skip tab */
  347.         sp = ap;
  348.         }
  349.         FieldsI = cfldI;
  350.         stateI = 1;
  351.         break;
  352.  
  353.     case 1:            /* second line of header */
  354.         for( cfldI = 0; cfldI < FieldsI; cfldI++ ) {
  355.         fldP = &FldTA[cfldI];            /* current field */
  356.         for( ap=sp; *ap && *ap != '\t'; ) ap++;    /* skip field */
  357.         chI = *ap;                /* save char */
  358.         *ap = 0;                /* null terminate */
  359.         fldP->FSymCP = DCDupCP( DCLwrPkCP(sp) );/* label */
  360.         if ( chI == '\t' ) ap++;        /* skip tab */
  361.         sp = ap;
  362.         }
  363.         stateI = 2;
  364.         break;
  365.  
  366.     case 2:            /* data */
  367.         if ( rowI > nlinL ) break;        /* too many lines */
  368.  
  369.         for( cfldI = 0; cfldI < FieldsI; cfldI++ ) {
  370.         fldP = &FldTA[cfldI];            /* current field */
  371.         for( ap=sp; *ap && *ap != '\t'; ) ap++;    /* skip field */
  372.         chI = *ap;                /* save char */
  373.         *ap = 0;                /* null terminate */
  374.         if ( cfldI )                 /* numeric data */
  375.              fldP->FDataFP[rowI] = strtod( sp, (char **)0 );
  376.         else FDateLP[rowI] = Date2BinL( sp );    /* date */
  377.         if ( chI == '\t' ) ap++;        /* skip tab */
  378.         sp = ap;
  379.         }
  380.         if ( SDateL <= FDateLP[rowI] && FDateLP[rowI] <= EDateL )
  381.         rowI++;        /* ok to load this row */
  382.         break;
  383.     }
  384.     }
  385.     FVecI = rowI;
  386.  
  387. Done:
  388.     if ( DBbufCP != (char *)0 ) free( (void *)DBbufCP );
  389.     if ( fp != (FILE *)0 )    fclose( fp );
  390.     if ( rcI == 0 ) {
  391.     fprintf( stdout, "%d records read from database <%s>\n",
  392.         FVecI, fnCP );
  393.     DBStrDateL = FDateLP[0];
  394.     DBEndDateL = FDateLP[FVecI-1];
  395.     strcpy( DBStrDateCA, Date2StrCP( DBStrDateL ) );
  396.     strcpy( DBEndDateCA, Date2StrCP( DBEndDateL ) );
  397.     OFStrDateL = DBStrDateL;
  398.     OFEndDateL = DBEndDateL;
  399.     strcpy( OFStrDateCA, DBStrDateCA );
  400.     strcpy( OFEndDateCA, DBEndDateCA );
  401.     SVStrDateL = DBStrDateL;
  402.     SVEndDateL = DBEndDateL;
  403.     strcpy( SVStrDateCA, DBStrDateCA );
  404.     strcpy( SVEndDateCA, DBEndDateCA );
  405.     NetDateL   = DBStrDateL;
  406.     strcpy( NetDateCA,   DBStrDateCA );
  407.     AnlDateL   = DBStrDateL;
  408.     strcpy( AnlDateCA,   DBStrDateCA );
  409.  
  410. #ifdef NLDS
  411.     NFieldFlagI = 0;
  412.     strcpy( &NFieldCA[0], "field" );
  413.     NStrDateL  = DBStrDateL;
  414.     NEndDateL  = DBEndDateL;
  415.     strcpy( NStrDateCA,  DBStrDateCA );
  416.     strcpy( NEndDateCA,  DBEndDateCA );
  417. #endif
  418.     }
  419.     return( rcI );
  420.  
  421. Error:
  422.     MsgER( buf );
  423.     rcI = -1;
  424.     goto Done;
  425. }
  426.  
  427. /************************************************************************
  428.  *                                    *
  429.  *    SearchDBI() - Search Data-base to see if symbol is there    *
  430.  *                                    *
  431.  ************************************************************************
  432.  */
  433.  
  434. int SearchDBI( tCP, fCP, ModeI )    /* search data-base */
  435. char        *tCP;        /* ticker pointer */
  436. char        *fCP;        /* label pointer */
  437. int         ModeI;        /* mode bits to look for */
  438. {
  439.     int         wxI;        /* work index */
  440.     FIELD    *p;        /* pointer to field */
  441.  
  442.     /* --- Search Data-base for symbol --- */
  443.  
  444.     if ( tCP == (char *)0 || *tCP == 0 ) return( -1 );
  445.     if ( fCP == (char *)0 || *fCP == 0 ) fCP = (char *)0;
  446.     for( wxI = 0; wxI < ASof(FldTA); wxI++ ) {
  447.     p = &FldTA[wxI];
  448.     if ( (p->FFlagI & ModeI) != 0 && strcmp( p->FTickCP, tCP ) == 0 ) {
  449.         if ( fCP == (char *)0 || strcmp( p->FSymCP, fCP ) == 0 )
  450.         return( wxI );
  451.     }
  452.     }
  453.     return( -1 );
  454. }
  455.  
  456. /************************************************************************
  457.  *                                    *
  458.  *    FieldParseI() - Parse a field definition            *
  459.  *                                    *
  460.  ************************************************************************
  461.     ticker.label[+/-offset]
  462.     rc:    -1 = syntax error
  463.          0 = success
  464.             fdTP-> results.  If not FD_FOUND, pointers
  465.                 should be looked up in the dictionary.
  466.  */
  467.  
  468. int FieldParseI( fdTP, fdCP )        /* parse a field */
  469. FDES        *fdTP;        /* pointer to field data structure */
  470. char        *fdCP;        /* pointer to field string */
  471. {
  472.     FIELD    *fTP;        /* field structure itself */
  473.     char    *tCP, *fCP;    /* ticker & field pointers */
  474.     char    *oCP;        /* offset */
  475.     char    *sp;        /* work pointer */
  476.     char    *dp;        /* destination pointer */
  477.     int         c;        /* work char */
  478.     int         OffsetI;    /* offset for computations */
  479.     int         wxI;        /* work offset */
  480.     static char     wbf[80];    /* work buffer */
  481.  
  482.     /* --- Convert to lower case & remove spaces --- */
  483.  
  484.     for( sp = fdCP, dp = &wbf[0]; (c = *sp)!=0; sp++ ) {
  485.     if ( 'A' <= c && c <= 'Z' ) c -= 'A'-'a';
  486.     if ( c > ' ' ) *dp++ = c;
  487.     }
  488.     *dp = 0;
  489.     if ( wbf[0] == 0 ) {
  490.     Error: return( -1 );        /* syntax error */
  491.     }
  492.  
  493.     tCP = fCP = oCP = (char *)0;    /* nothing yet */
  494.     for( sp = &wbf[0]; *sp; sp++ ) {
  495.     switch( *sp ) {
  496.     default:
  497.         if ( tCP != (char *)0 ) goto Error;
  498.         tCP = sp;
  499.         while( *sp > ' ' && *sp != '.' && *sp != '(' && *sp != '[' &&
  500.            *sp != ')' && *sp != ']' ) sp++;
  501.         sp--;
  502.         break;
  503.  
  504.     case '.':
  505.         if ( tCP == (char *)0 || fCP != (char *)0 ) goto Error;
  506.         *sp++ = 0;
  507.         fCP = sp;
  508.         while( *sp && *sp != '(' && *sp != '[' && *sp != '.' ) sp++;
  509.         sp--;
  510.         break;
  511.  
  512.     case '[': case '(':
  513.         if ( tCP == (char *)0 || oCP != (char *)0 ) goto Error;
  514.         *sp++ = 0;        /* null terminate prior item */
  515.         oCP = sp;
  516.         if ( *sp == '+' || *sp == '-' ) sp++;
  517.         OffsetI = 0;
  518.         while( '0' <= *sp && *sp <= '9' ) { sp++; OffsetI++; }
  519.         if ( OffsetI == 0 || (*sp != ')' && *sp != ']') ) goto Error;
  520.         *sp = 0;
  521.         break;
  522.  
  523.     case '+': case '-':
  524.     case ']': case ')':
  525.         goto Error;
  526.     }
  527.     }
  528.     if ( tCP == (char *)0 ) goto Error;
  529.     if ( fCP != (char *)0 && *fCP == 0 ) goto Error;
  530.     OffsetI = (oCP == (char *)0)? 0:atoi(oCP);
  531.  
  532.     /* --- Search Data-base for symbol --- */
  533.  
  534.     fdTP->FldXI       = 0;
  535.     fdTP->FFlagI   = FD_TICK;
  536.     if ( (wxI = SearchDBI( tCP, fCP, FF_DB )) >= 0 ) {
  537.     fdTP->FldXI     = wxI;
  538.     fdTP->FFieldTP  = &FldTA[wxI];
  539.     fdTP->FFlagI   |=  FD_FOUND;
  540.     tCP        =  FldTA[wxI].FTickCP;
  541.     if ( fCP ) fCP    =  FldTA[wxI].FSymCP;
  542.     }
  543.     fdTP->FTickCP  =  tCP;
  544.     fdTP->FSymCP   =  fCP;
  545.     fdTP->FOffI    =  OffsetI;
  546.     if ( fCP != (char *)0 ) fdTP->FFlagI |= FD_SYM;
  547.     if ( oCP != (char *)0 ) fdTP->FFlagI |= FD_OFF;
  548.  
  549.     return( 0 );
  550. }
  551.  
  552. /************************************************************************
  553.  *                                    *
  554.  *    FieldBuildI() - Build a new field in the data-base        *
  555.  *                                    *
  556.  ************************************************************************
  557.     return:    >= 0, field index
  558.         <  0, error
  559.  */
  560.  
  561. int FieldBuildI( tCP, fCP, trnCP )    /* build a field */
  562. char        *tCP;        /* ticker of input field */
  563. char        *fCP;        /* label of input field */
  564. char        *trnCP;        /* transformation */
  565. {
  566.     int         cI;        /* column index */
  567.     int         wI;        /* work int */
  568.     FIELD    *p;        /* pointer to field */
  569.     float    *pF;        /* float pointer */
  570.     char     buf[80];    /* work buf */
  571.  
  572.     if ( (cI = FieldsI) >= ASof(FldTA) )
  573.     return( MsgER( "Maximum allowable fields in data-base already" ) );
  574.  
  575.     p = &FldTA[cI];
  576.     wI = FVecI * sizeof(float);
  577.     if ( (pF = (float *)malloc( (unsigned)wI )) == (float *)0 )
  578.     return( MsgER( "Could not allocate space for data vector" ) );
  579.  
  580.     FieldsI++;
  581.     p->FDataFP = pF;
  582.     p->FFlagI = FF_DRV;
  583.     strcpy( buf, tCP );
  584.     strcat( buf, "_" );
  585.     strcat( buf, fCP );
  586.     p->FTickCP = DCDupCP( buf );
  587.     sprintf( buf, "%s_%d", trnCP, cI );
  588.     p->FSymCP  = DCDupCP( buf );
  589.  
  590.     for( wI = 0; wI < FVecI; wI++ ) *pF++ = 0.0;
  591.  
  592.     return( cI );
  593. }
  594.  
  595. /************************************************************************
  596.  *                                    *
  597.  *            Action Routines                    *
  598.  *                                    *
  599.  ************************************************************************
  600.  */
  601.  
  602. /* --- General purpose error messages --- */
  603.  
  604. MustLoadDBI() { MsgER( "Must load data-base first" ); return(-1); }
  605.  
  606. /* --- Basic Action Commands --- */
  607.  
  608. /* --- CMDLoad() ---
  609.  *    Load a Data-Base into memory
  610.  */
  611.  
  612. INPUT LISTLoad[] = {
  613.     ISTR( DBFile, IO_FNM ),
  614.     ISTR( DBStrDateCA, IO_DTE|IO_OPT ), 
  615.     ISTR( DBEndDateCA, IO_DTE|IO_OPT ),
  616.     INUL
  617. };
  618.  
  619. int CMDLoad( cmdP, cntL, eFP )    /* load a data-base file */
  620. CMD        *cmdP;        /* pointer to command table */
  621. long         cntL;        /* # of items converted */
  622. FILE        *eFP;        /* error file pointer */
  623. {
  624.     int         rcI;        /* return code */
  625.  
  626.     DBEndDateL = cntL < 3? 0x19991231L:Date2BinL(DBEndDateCA);
  627.     DBStrDateL = cntL < 2? 0x19000101L:Date2BinL(DBStrDateCA);
  628.     rcI = LoadDBI( DBFile, DBStrDateL, DBEndDateL );
  629.     return( rcI );
  630. }
  631.  
  632. /* --- CMDInterp() ---
  633.  *    Interpolate zero data-items in each column.
  634.  */
  635.  
  636. int CMDInterp( cmdP, cntL, eFP )
  637. CMD        *cmdP;        /* pointer to command table */
  638. long         cntL;        /* # of items converted */
  639. FILE        *eFP;        /* error file pointer */
  640. {
  641.     int         cI, rI;    /* column index, row index */
  642.     int         aI;        /* alt index */
  643.     float     aF, bF, iF;    /* start / end float values */
  644.     FIELD    *p;        /* field pointer */
  645.     float    *pF;        /* data pointer */
  646.  
  647.     if ( FieldsI <= 0 ) return( MustLoadDBI() );
  648.  
  649.     for( cI = 1; cI < FieldsI; cI++ ) {
  650.     p = &FldTA[cI];
  651.     if ( (p->FFlagI & FF_DB)    == 0 ||
  652.          (p->FFlagI & FF_INTRP) != 0 ) continue;
  653.  
  654.     pF = p->FDataFP;
  655.     for( rI = 0; rI < FVecI; rI++ ) {
  656.         if ( pF[rI] == 0.0 ) {
  657.         for( aI = rI; aI < FVecI; aI++ )
  658.             /* --- find non-zero val --- */
  659.             if ( pF[aI] != 0.0 ) break;
  660.         if ( rI == 0 && aI < FVecI ) {
  661.             /* --- zeros at start of data --- */
  662.             bF = pF[aI];
  663.             while( rI < aI ) pF[rI++] = bF;
  664.         } else if ( rI > 0 && aI >= FVecI ) {
  665.             /* --- zeros at end of data --- */
  666.             aF = pF[rI-1];
  667.             while( rI < aI ) pF[rI++] = aF;
  668.         } else if ( rI > 0 && aI < FVecI ) {
  669.             /* --- zeros in middle of data --- */
  670.             aF = pF[rI-1];    /* rI=0 caught in first case */
  671.             bF = pF[aI];
  672.             iF = (bF - aF) / (aI - rI + 1);
  673.             for( aF += iF; rI < aI; rI++, aF += iF ) pF[rI] = aF;
  674.         } else break;    /* --- all zeros --- */
  675.         }
  676.     }
  677.     p->FFlagI |= FF_INTRP;
  678.     }
  679.     return( 0 );
  680. }
  681.  
  682. /* --- CMDScale() ---
  683.  *    Scale ALL data into the specified range
  684.  */
  685.  
  686. INPUT LISTScale[] = {
  687.     IVAL( MinScaleD, IO_FLT|IO_OPT, FMT_FLT2, 0 ),
  688.     IVAL( MaxScaleD, IO_FLT|IO_OPT, FMT_FLT2, 0 ),
  689.     INUL
  690. };
  691.  
  692. int CMDScale( cmdP, cntL, eFP )
  693. CMD        *cmdP;        /* pointer to command table */
  694. long         cntL;        /* # of items converted */
  695. FILE        *eFP;        /* error file pointer */
  696. {
  697.     int         cI, rI;    /* column index, row index */
  698.     FIELD    *p;        /* pointer to field item */
  699.     float    *pF;        /* pointer to data */
  700.     double     MinD, MaxD;    /* work variables */
  701.     double     wD;        /* work double */
  702.     double     ScD, OfD;    /* scale & offset for this column */
  703.  
  704.     if ( FieldsI <= 0 ) return( MustLoadDBI() );
  705.  
  706.     for( cI = 1; cI < FieldsI; cI++ ) {
  707.     p = &FldTA[cI];
  708.     if ( (p->FFlagI & FF_DRV) == 0 )
  709.         continue;        /* only scale derived fields */
  710.     pF = p->FDataFP;
  711.     MinD = MaxD = pF[0];
  712.     for( pF++, rI = 1; rI < FVecI; rI++, pF++ ) {
  713.         wD = *pF;
  714.         if ( wD < MinD ) MinD = wD;
  715.         if ( wD > MaxD ) MaxD = wD;
  716.     }
  717.  
  718.     if ( MinD == MaxD ) {
  719.         ScD = OfD = 0.0;
  720.     } else {
  721.         ScD = (MaxScaleD - MinScaleD) / (MaxD - MinD);
  722.         OfD = (MinScaleD - MinD * ScD);
  723.         if ( fabs(ScD-1.0) < 1.e-3 && fabs(OfD) < 1.e-3 )
  724.         continue;        /* already scaled */
  725.     }
  726.  
  727.     for( pF = p->FDataFP, rI = 0; rI < FVecI; rI++, pF++ )
  728.         *pF = ScD * (*pF) + OfD;
  729.     }
  730.     return( 0 );
  731. }
  732.  
  733. /* --- Format a Field of Data with minimum trailing zeros --- */
  734.  
  735. static char *FmtFltCP( fmtCP, vD )    /* format a floating point number */
  736. char        *fmtCP;        /* format */
  737. double         vD;        /* double value */
  738. {
  739.     char    *sp;        /* work pointer */
  740.     static char     buf[80]={0};    /* work buffer */
  741.  
  742.     sprintf( buf, fmtCP, vD );    /* format the number */
  743.     for( sp = &buf[0]; *sp; sp++ )    /* check for decimal points */
  744.     if ( *sp == '.' ) break;
  745.     if ( *sp != 0 ) {
  746.     for( sp = &buf[strlen(buf)-1]; sp > &buf[0]; sp-- )
  747.         if ( *sp != '0' ) break;
  748.     if ( *sp == '.' ) sp--;
  749.     sp[1] = 0;
  750.     }
  751.     return( &buf[0] );
  752. }
  753.  
  754. /* --- Generate an output file --- */
  755.  
  756. int WriteFile( fnCP, StrDL, EndDL, ModeI )    /* create an output file */
  757. char        *fnCP;        /* name of file to create */
  758. long         StrDL, EndDL;    /* start & end dates */
  759. int         ModeI;        /* types of items to include */
  760. {
  761.     FILE    *ofFP=0;    /* output file pointer */
  762.     int         cI, rI;    /* column index, row index */
  763.     int         ffI;        /* first field flag */
  764.     FIELD    *p;        /* pointer to field item */
  765.     float    *pF;        /* pointer to data */
  766.     double     wD;        /* work double */
  767.     long     ctL;        /* current time */
  768.     char    *sp, *dp;    /* work pointers */
  769.     char     buf[80];    /* date buffer */
  770.  
  771.     if ( FieldsI <= 0 ) return( MustLoadDBI() );
  772.  
  773.     /* --- Sanity check:  Is there anything to do? --- */
  774.  
  775.     if ( StrDL > EndDL ) {
  776.     ctL = StrDL; StrDL = EndDL; EndDL = ctL;
  777.     }
  778.  
  779.     for( rI = 0; rI < FVecI; rI++ )
  780.     if ( StrDL <= FDateLP[rI] && FDateLP[rI] <= EndDL )
  781.         break;        /* found something in range */
  782.     if ( rI >= FVecI )
  783.     return( MsgER( "Nothing in date range specified" ) );
  784.  
  785.     for( cI = 1; cI < FieldsI; cI++ )
  786.     if ( (FldTA[cI].FFlagI & ModeI) != 0 ) break;
  787.     if ( cI >= FieldsI )
  788.     return( MsgER( "Nothing to write" ) );
  789.  
  790.     /* --- Open the file --- */
  791.  
  792.     if ( (ofFP = fopen( fnCP, "w" )) == (FILE *)0 )
  793.     return( MsgER( "Could not open output file" ) );
  794.  
  795.     /* --- Write out the data itself --- */
  796.  
  797.     time( &ctL );
  798.     for( dp=buf, sp=asctime(localtime(&ctL)); *sp; sp++ )
  799.     if ( *sp >= ' ' ) *dp++ = *sp;
  800.     *dp = 0;
  801.     fprintf( ofFP, "!%s (%s)\n", buf, fnCP );
  802.  
  803.     for( ffI = 0, cI = 0; cI < FieldsI; cI++ ) {
  804.     p = &FldTA[cI];
  805.     if ( (p->FFlagI & ModeI) != 0 ) {
  806.         fputs( ffI?"\t":((ModeI & FF_DB)==0?"!":""), ofFP );
  807.         fputs( p->FTickCP, ofFP );
  808.         ffI++;
  809.     }
  810.     }
  811.     fputs( "\n", ofFP );
  812.  
  813.     for( ffI = 0, cI = 0; cI < FieldsI; cI++ ) {
  814.     p = &FldTA[cI];
  815.     if ( (p->FFlagI & ModeI) != 0 ) {
  816.         fputs( ffI?"\t":((ModeI & FF_DB)==0?"!":""), ofFP );
  817.         fputs( p->FSymCP, ofFP );
  818.         ffI++;
  819.     }
  820.     }
  821.     fputs( "\n", ofFP );
  822.  
  823.     for( ; rI < FVecI; rI++ ) {
  824.     if ( FDateLP[rI] > EndDL ) break;
  825.     for( ffI = 0, cI = 0; cI < FieldsI; cI++ ) {
  826.         p = &FldTA[cI];
  827.         if ( (p->FFlagI & ModeI) != 0 ) {
  828.         wD = p->FDataFP[rI];
  829.         fputs( ffI?"\t":"", ofFP );
  830.         if ( cI == 0 ) {
  831.             fputs( Date2StrCP( FDateLP[rI] ), ofFP );
  832.         } else {
  833.             fputs( FmtFltCP( "%.4f", wD ), ofFP );
  834.         }
  835.         ffI++;
  836.         }
  837.     }
  838.     fputs( "\n", ofFP );
  839.     }
  840.     fclose( ofFP );
  841.     return( 0 );
  842. }
  843.  
  844. /* --- Create a "transformed" data file --- */
  845.  
  846. INPUT LISTTrans[] = {
  847.     ISTR( OutFile, IO_FNM ),
  848.     ISTR( OFStrDateCA, IO_DTE|IO_OPT),
  849.     ISTR( OFEndDateCA, IO_DTE|IO_OPT),
  850.     INUL
  851. };
  852.  
  853. int CMDTrans( cmdP, cntL, eFP )
  854. CMD        *cmdP;        /* pointer to command table */
  855. long         cntL;        /* # of items converted */
  856. FILE        *eFP;        /* error file pointer */
  857. {
  858.     int         rcI;        /* return code */
  859.  
  860.     OFStrDateL = Date2BinL( OFStrDateCA );
  861.     OFEndDateL = Date2BinL( OFEndDateCA );
  862.     rcI = WriteFile( OutFile, OFStrDateL, OFEndDateL, FF_DRV );
  863.     return( rcI );
  864. }
  865.  
  866. /* --- Save output of data-base in a new file --- */
  867.  
  868. INPUT LISTSave[] = {
  869.     ISTR( SaveFile, IO_FNM ),
  870.     ISTR( SVStrDateCA, IO_DTE|IO_OPT),
  871.     ISTR( SVEndDateCA, IO_DTE|IO_OPT),
  872.     INUL
  873. };
  874.  
  875. int CMDSave( cmdP, cntL, eFP )
  876. CMD        *cmdP;        /* pointer to command table */
  877. long         cntL;        /* # of items converted */
  878. FILE        *eFP;        /* error file pointer */
  879. {
  880.     int         rcI;        /* return code */
  881.  
  882.     if ( cntL > 1 ) {
  883.     SVStrDateL = Date2BinL( SVStrDateCA );
  884.     SVEndDateL = Date2BinL( SVEndDateCA );
  885.     } else {
  886.     SVStrDateL = 0L;
  887.     SVEndDateL = 0x19991231L;
  888.     }
  889.     rcI = WriteFile( SaveFile, SVStrDateL, SVEndDateL, FF_DRV|FF_DB );
  890.     return( rcI );
  891. }
  892.  
  893. /* --- CMDLog() ---
  894.  *    Change / set the output log file
  895.  */
  896.  
  897. int CMDLog( cmdP, cntL, eFP )
  898. CMD        *cmdP;        /* pointer to command table */
  899. long         cntL;        /* # of items converted */
  900. FILE        *eFP;        /* error file pointer */
  901. {
  902.     FILE    *fp;        /* work file pointer */
  903.  
  904.     if ( strcmp( "none", LogFile ) == 0 ) {
  905.     if ( LogFP != (FILE *)0 ) fclose( LogFP );
  906.     LogFP = (FILE *)0;
  907.     return( 0 );
  908.     }
  909.  
  910.     if ( (fp = fopen( LogFile, "w" )) == (FILE *)0 )
  911.     return( MsgER( "Could not open new log file. Using old one." ) );
  912.  
  913.     if ( LogFP ) fclose( LogFP );
  914.     LogFP = fp;
  915.  
  916.     return( 0 );
  917. }
  918.  
  919. int CheckPointLogFile()    /* check point the log file */
  920. {   /* --- Temporarily close log file to preserve anything in it --- */
  921.     if ( LogFP ) {
  922.     fflush( LogFP );
  923.     fclose( LogFP );
  924.     LogFP = fopen( LogFile, "a" );
  925.     return( 1 );
  926.     }
  927.     return( 0 );
  928. }
  929.  
  930. /* --- CMDDict() ---
  931.  *    Print out the dictionary of data in memory
  932.  */
  933.  
  934. int CMDDict( cmdP, cntL, eFP )
  935. CMD        *cmdP;        /* pointer to command table */
  936. long         cntL;        /* # of items converted */
  937. FILE        *eFP;        /* error file pointer */
  938. {
  939.     FIELD    *p;        /* pointer to field item */
  940.     int         cI;        /* column */
  941.     char     buf[80];    /* work buffer */
  942.  
  943.     if ( FieldsI <= 1 )
  944.     return( MustLoadDBI() );
  945.  
  946.     sprintf( buf, "--- Current Dictionary ---\n" );
  947.     fputs( buf, eFP );
  948.     if ( LogFP ) fputs( buf, LogFP );
  949.     for( cI = 1; cI < FieldsI; cI++ ) {
  950.     p = &FldTA[cI];
  951.     sprintf( buf, "  %s.%s", p->FTickCP, p->FSymCP );
  952.     switch( p->FFlagI & (FF_DB | FF_DRV | FF_INTRP) ) {
  953.     case FF_DB:        strcat( buf, " (data)" );    break;
  954.     case FF_DB|FF_INTRP:    strcat( buf, " (data,interpolated)" ); break;
  955.     case FF_DRV:        strcat( buf, " (transform)" );
  956.     }
  957.     strcat( buf, "\n" );
  958.     fputs( buf, eFP );
  959.     if ( LogFP ) fputs( buf, LogFP );
  960.     }
  961.     fflush( eFP );
  962.     if ( LogFP ) fflush( LogFP );
  963.  
  964.     return( 0 );
  965. }
  966.  
  967. /* --- CMDQuit() ---
  968.  *    Terminate the program.
  969.  */
  970.  
  971. int QuitI    = {0};        /* quit flag */
  972.  
  973. int CMDQuit( cmdP, cntL, eFP )
  974. CMD        *cmdP;        /* pointer to command table */
  975. long         cntL;        /* # of items converted */
  976. FILE        *eFP;        /* error file pointer */
  977. {
  978.     QuitI = 1;
  979.     return( 0 );
  980. }
  981.  
  982. /* --- DoCommandsI() --- Execute commands from the command line
  983.  *   until "quit" is encountered.  Provide for recursive calls
  984.  *   from other routines to switch to alternate command tables &
  985.  *   sub-functions.
  986.  */
  987.  
  988. int DoCommandsI( ct, PromptSP )        /* execute commands */
  989. CMD        *ct;        /* command table */
  990. char        *PromptSP;    /* prompt */
  991. {
  992.     int         rc;        /* return code */
  993.     char    *sp;        /* string pointer */
  994.     char    *wsp;        /* work string pointer */
  995.     char     buf[80];    /* work buffer */
  996.     int         QuitSaveI;    /* save old quit flag */
  997.  
  998.     QuitSaveI = QuitI;
  999.     for( QuitI = 0; QuitI == 0 ;) {
  1000.     fputs( PromptSP, stdout );
  1001.     fputs( "> ", stdout );
  1002.     fflush( stdout );
  1003.     sp = fgets( buf, sizeof(buf), stdin );
  1004.     wsp = sp;
  1005.     if ( LogFP ) {
  1006.         fprintf( LogFP, "%s> %s", PromptSP, buf );
  1007.         rc = ParseLineI( &wsp, &ct[0], LogFP, 1 );    /* syntax check */
  1008.         wsp = sp;
  1009.         CheckPointLogFile();
  1010.     }
  1011.     rc = ParseLineI( &wsp, &ct[0], stdout, 1 );    /* syntax check */
  1012.     if ( rc == 0 ) {
  1013.         wsp = sp;
  1014.         rc = ParseLineI( &wsp, &ct[0], stdout, 0 );
  1015.         CheckPointLogFile();
  1016.     }
  1017.     }
  1018.     QuitI = QuitSaveI;
  1019.     return( 0 );
  1020. }
  1021.  
  1022. /* --- Transformations --- */
  1023.  
  1024. INPUT LISTFldDay[] = {        /* field,days */
  1025.     ISTR( XFieldCA, IO_FLD ),
  1026.     IVAL( SpanI,    IO_INT,FMT_INTU, 0 ),
  1027.     INUL
  1028. };
  1029. INPUT LISTFFDay[] = {        /* field,field,days */
  1030.     ISTR( XFieldCA, IO_FLD ),
  1031.     ISTR( YFieldCA, IO_FLD ),
  1032.     IVAL( SpanI,    IO_INT,FMT_INTU, 0 ),
  1033.     INUL
  1034. };
  1035. INPUT LISTOFFDay[] = {        /* [field],field,days */
  1036.     ISTR( XFieldCA, IO_FLD|IO_OPT ),
  1037.     ISTR( YFieldCA, IO_FLD ),
  1038.     IVAL( SpanI,    IO_INT,FMT_INTU, 0 ),
  1039.     INUL
  1040. };
  1041. INPUT LISTStoch[] = {        /* field,days[,smooth] */
  1042.     ISTR( XFieldCA, IO_FLD ),
  1043.     IVAL( SpanI,    IO_INT,FMT_INTU, 0 ),
  1044.     IVAL( SmoothI,  IO_INT|IO_OPT,FMT_INTU, 0 ),
  1045.     INUL
  1046. };
  1047.  
  1048. int FieldSetupI( ppsF, ppdF, plenI, trnCP )
  1049. float    **ppsF;        /* pointer to source data pointer */
  1050. float    **ppdF;        /* pointer to dest data pointer */
  1051. int     *plenI;    /* pointer to length */
  1052. char     *trnCP;    /* pointer to transform type */
  1053. {
  1054.     float    *psF, *pdF;    /* source / destination data */
  1055.     int         dfXI;        /* destination field index */
  1056.     int         lenI;        /* work length */
  1057.     int         offI;        /* offset */
  1058.     int         wI;        /* absolute offset */
  1059.  
  1060.     if ( FieldParseI( &XFieldT, XFieldCA ) )
  1061.     return( MsgER( "Could not parse field" ) );
  1062.     if ( (XFieldT.FFlagI & FD_FOUND) == 0 )
  1063.     return( MsgER( "Field specified could not be found" ) );
  1064.  
  1065.     offI   = XFieldT.FOffI;
  1066.     wI     = offI < 0? (-offI):(offI);
  1067.     if ( (FVecI - wI) <= 2 )
  1068.     return( MsgER( "Field offset is too big for data" ) );
  1069.     psF = XFieldT.FFieldTP->FDataFP;
  1070.  
  1071.     dfXI = FieldBuildI( XFieldT.FTickCP, XFieldT.FSymCP, trnCP );
  1072.     if ( dfXI < 0 ) return( dfXI );
  1073.     pdF = FldTA[dfXI].FDataFP;
  1074.  
  1075.     lenI = FVecI - wI;        /* adjust length */
  1076.     if (      offI > 0 ) psF += wI;
  1077.     else if ( offI < 0 ) pdF += wI;
  1078.  
  1079.     *ppsF = psF;
  1080.     *ppdF = pdF;
  1081.     *plenI = lenI;
  1082.     return( 0 );
  1083. }
  1084.  
  1085. int FieldSetup2I( pps1F, pps2F, ppdF, plenI, trnCP, Opt1I )
  1086. float    **pps1F;    /* pointer to first source data pointer */
  1087. float    **pps2F;    /* pointer to second source data pointer */
  1088. float    **ppdF;        /* pointer to dest data pointer */
  1089. int     *plenI;    /* pointer to length */
  1090. char     *trnCP;    /* pointer to transform type */
  1091. int      Opt1I;    /* =1, first arg is NOT there */
  1092. {
  1093.     float    *ps1F,*ps2F,*pdF;    /* source / destination data */
  1094.     int         dfXI;            /* destination field index */
  1095.     int         lenI;            /* work length */
  1096.     int         off1I, off2I;        /* offset */
  1097.     int         offdI;            /* offset of destination */
  1098.     int         wI;            /* work integer */
  1099.  
  1100.     /* --- Parse First Field --- */
  1101.  
  1102.     if ( Opt1I ) {
  1103.     ps1F = (float *)0;
  1104.     off1I = 0;
  1105.     } else {
  1106.     if ( FieldParseI( &XFieldT, XFieldCA ) )
  1107.         return( MsgER( "Could not parse field" ) );
  1108.     if ( (XFieldT.FFlagI & FD_FOUND) == 0 )
  1109.         return( MsgER( "Field specified could not be found" ) );
  1110.  
  1111.     off1I  = XFieldT.FOffI;
  1112.     wI     = off1I < 0? (-off1I):(off1I);
  1113.     if ( (FVecI - wI) <= 2 )
  1114.         return( MsgER( "Field offset 1 is too big for data" ) );
  1115.     ps1F = XFieldT.FFieldTP->FDataFP;
  1116.     }
  1117.  
  1118.     /* --- Parse Second Field --- */
  1119.  
  1120.     if ( FieldParseI( &YFieldT, YFieldCA ) )
  1121.     return( MsgER( "Could not parse second field" ) );
  1122.     if ( (YFieldT.FFlagI & FD_FOUND) == 0 )
  1123.     return( MsgER( "Second field could not be found" ) );
  1124.  
  1125.     off2I  = YFieldT.FOffI;
  1126.     wI     = off2I < 0? (-off2I):(off2I);
  1127.     if ( (FVecI - wI) <= 2 )
  1128.     return( MsgER( "Field offset 2 is too big for data" ) );
  1129.     ps2F = YFieldT.FFieldTP->FDataFP;
  1130.  
  1131.     /* --- Build result field --- */
  1132.  
  1133.     dfXI = FieldBuildI( YFieldT.FTickCP, YFieldT.FSymCP, trnCP );
  1134.     if ( dfXI < 0 ) return( dfXI );
  1135.     pdF = FldTA[dfXI].FDataFP;
  1136.     lenI = FVecI;
  1137.  
  1138.     /* --- adjust the pointers as required --- */
  1139.  
  1140.     if ( ps1F == (float *)0 ) {
  1141.     wI = (off2I < 0? (-off2I):(off2I));
  1142.     lenI   -= wI;        /* adjust length */
  1143.     if (      off2I > 0 ) ps2F += wI;
  1144.     else if ( off2I < 0 ) pdF  += wI;
  1145.     } else {
  1146.     if (      off1I <= off2I && off1I < 0 )    offdI = (-off1I);
  1147.     else if ( off1I >  off2I && off2I < 0 )    offdI = (-off2I);
  1148.     else                    offdI = 0;
  1149.     off1I += offdI;
  1150.     off2I += offdI;
  1151.     wI = offdI;
  1152.     if ( wI < off1I ) wI = off1I;
  1153.     if ( wI < off2I ) wI = off2I;    /* wI is largest offset */
  1154.     lenI = FVecI - wI;        /* adjusted length */
  1155.     ps1F += off1I;            /* adjusted pointers */
  1156.     ps2F += off2I;
  1157.     pdF  += offdI;
  1158.     }
  1159.  
  1160.     *pps1F = ps1F;
  1161.     *pps2F = ps2F;
  1162.     *ppdF  = pdF;
  1163.     *plenI = lenI;
  1164.     return( 0 );
  1165. }
  1166.  
  1167. int CMDMovAvg( cmdP, cntL, eFP )    /* compute moving average */
  1168. CMD        *cmdP;        /* pointer to command table */
  1169. long         cntL;        /* # of items converted */
  1170. FILE        *eFP;        /* error file pointer */
  1171. {
  1172.     int         rcI;        /* work return code */
  1173.     float    *psF, *pdF;    /* source / destination data */
  1174.     int         lenI;        /* work length */
  1175.  
  1176.     if ( FieldSetupI( &psF, &pdF, &lenI, "movavg" ) < 0 )
  1177.     return( -1 );
  1178.  
  1179.     MovAvgV( pdF, psF, lenI, SpanI );
  1180.     return( 0 );
  1181. }
  1182.  
  1183.  
  1184. int CMDRSI( cmdP, cntL, eFP )    /* relative strength index */
  1185. CMD        *cmdP;        /* pointer to command table */
  1186. long         cntL;        /* # of items converted */
  1187. FILE        *eFP;        /* error file pointer */
  1188. {
  1189.     float    *psF, *pdF;    /* source / destination data */
  1190.     int         lenI;        /* work length */
  1191.  
  1192.     if ( FieldSetupI( &psF, &pdF, &lenI, "rsi" ) < 0 )
  1193.     return( -1 );
  1194.  
  1195.     RSIV( pdF, psF, lenI, SpanI );
  1196.     return( 0 );
  1197. }
  1198.  
  1199.  
  1200. int CMDStoch( cmdP, cntL, eFP )    /* stochastic */
  1201. CMD        *cmdP;        /* pointer to command table */
  1202. long         cntL;        /* # of items converted */
  1203. FILE        *eFP;        /* error file pointer */
  1204. {
  1205.     float    *psF, *pdF;    /* source / destination data */
  1206.     int         lenI;        /* work length */
  1207.  
  1208.     if ( FieldSetupI( &psF, &pdF, &lenI, "stoch" ) < 0 )
  1209.     return( -1 );
  1210.  
  1211.     StochasticV( pdF, psF, lenI, SpanI, SmoothI );
  1212.     return( 0 );
  1213. }
  1214.  
  1215.  
  1216. int CMDVol( cmdP, cntL, eFP )    /* volatility */
  1217. CMD        *cmdP;        /* pointer to command table */
  1218. long         cntL;        /* # of items converted */
  1219. FILE        *eFP;        /* error file pointer */
  1220. {
  1221.     float    *psF, *pdF;    /* source / destination data */
  1222.     int         lenI;        /* work length */
  1223.  
  1224.     if ( FieldSetupI( &psF, &pdF, &lenI, "vol" ) < 0 )
  1225.     return( -1 );
  1226.  
  1227.     VolatilityV( pdF, psF, lenI, SpanI );
  1228.     return( 0 );
  1229. }
  1230.  
  1231.  
  1232. int CMDHLVol( cmdP, cntL, eFP )    /* high/low volatility */
  1233. CMD        *cmdP;        /* pointer to command table */
  1234. long         cntL;        /* # of items converted */
  1235. FILE        *eFP;        /* error file pointer */
  1236. {
  1237.     float    *psF, *pdF;    /* source / destination data */
  1238.     float    *qsF;        /* alt source data */
  1239.     int         lenI;        /* work length */
  1240.  
  1241.     if ( FieldSetup2I( &psF, &qsF, &pdF, &lenI, "hlvol", 0 ) < 0 )
  1242.     return( -1 );
  1243.  
  1244.     HighLowVolatilityV( pdF, psF, qsF, lenI, SpanI );
  1245.     return( 0 );
  1246. }
  1247.  
  1248.  
  1249. int CMDSlope( cmdP, cntL, eFP )    /* regression slope */
  1250. CMD        *cmdP;        /* pointer to command table */
  1251. long         cntL;        /* # of items converted */
  1252. FILE        *eFP;        /* error file pointer */
  1253. {
  1254.     float    *psF, *pdF;    /* source / destination data */
  1255.     float    *qsF;        /* alt source data */
  1256.     int         lenI;        /* work length */
  1257.     int         Opt1I;        /* optional input parameter */
  1258.  
  1259.     Opt1I = cntL < 3? 1:0;
  1260.     if ( FieldSetup2I( &psF, &qsF, &pdF, &lenI, "slope", Opt1I ) < 0 )
  1261.     return( -1 );
  1262.  
  1263.     SlidingRegressionV( pdF, (float *)0, psF, qsF, lenI, SpanI );
  1264.     return( 0 );
  1265. }
  1266.  
  1267.  
  1268. int CMDInter( cmdP, cntL, eFP )    /* regression intercept */
  1269. CMD        *cmdP;        /* pointer to command table */
  1270. long         cntL;        /* # of items converted */
  1271. FILE        *eFP;        /* error file pointer */
  1272. {
  1273.     float    *psF, *pdF;    /* source / destination data */
  1274.     float    *qsF;        /* alt source data */
  1275.     int         lenI;        /* work length */
  1276.     int         Opt1I;        /* optional input parameter */
  1277.  
  1278.     Opt1I = cntL < 3? 1:0;
  1279.     if ( FieldSetup2I( &psF, &qsF, &pdF, &lenI, "intercept", Opt1I ) < 0 )
  1280.     return( -1 );
  1281.  
  1282.     SlidingRegressionV( (float *)0, pdF, psF, qsF, lenI, SpanI );
  1283.     return( 0 );
  1284. }
  1285.  
  1286.  
  1287. int CMDCorr( cmdP, cntL, eFP )    /* rolling correlation */
  1288. CMD        *cmdP;        /* pointer to command table */
  1289. long         cntL;        /* # of items converted */
  1290. FILE        *eFP;        /* error file pointer */
  1291. {
  1292.     float    *psF, *pdF;    /* source / destination data */
  1293.     float    *qsF;        /* alt source data */
  1294.     int         lenI;        /* work length */
  1295.  
  1296.     if ( FieldSetup2I( &psF, &qsF, &pdF, &lenI, "corr", 0 ) < 0 )
  1297.     return( -1 );
  1298.  
  1299.     RollingCorrelationV( pdF, psF, qsF, lenI, SpanI );
  1300.     return( 0 );
  1301. }
  1302.  
  1303.  
  1304. int CMDRaw( cmdP, cntL, eFP )    /* raw data (typically offset) */
  1305. CMD        *cmdP;        /* pointer to command table */
  1306. long         cntL;        /* # of items converted */
  1307. FILE        *eFP;        /* error file pointer */
  1308. {
  1309.     float    *psF, *pdF;    /* source / destination data */
  1310.     int         lenI;        /* work length */
  1311.  
  1312.     if ( FieldSetupI( &psF, &pdF, &lenI, "raw" ) < 0 )
  1313.     return( -1 );
  1314.     for( ; lenI; lenI-- ) *pdF++ = *psF++;
  1315.  
  1316.     return( 0 );
  1317. }
  1318.  
  1319. /* --- Analysis Section --- */
  1320.  
  1321. #define    RPT_SUMMARY    0x0001    /* summary */
  1322. #define    RPT_TRANS    0x0002    /* detail transactions */
  1323. int    ReportFI    = {RPT_SUMMARY|RPT_TRANS};    /* report flags */
  1324.  
  1325. LIT LITReport[] = {
  1326.   { "summary",    RPT_SUMMARY },
  1327.   { "detail",    RPT_TRANS },
  1328.   { "all",    RPT_SUMMARY|RPT_TRANS },
  1329.   { 0 }
  1330. };
  1331.  
  1332. INPUT LISTLong[] = {
  1333.     IVAL( LongLowD,  IO_FLT,FMT_FLT2, 0 ),
  1334.     IVAL( LongHighD, IO_FLT,FMT_FLT2, 0 ),
  1335.     INUL
  1336. };
  1337. INPUT LISTShort[] = {
  1338.     IVAL( ShortLowD,  IO_FLT,FMT_FLT2, 0 ),
  1339.     IVAL( ShortHighD, IO_FLT,FMT_FLT2, 0 ),
  1340.     INUL
  1341. };
  1342. INPUT LISTNet[] = {
  1343.     ISTR( NetFileCA, IO_FNM ),
  1344.     ISTR( NetDateCA, IO_DTE ),
  1345.     INUL
  1346. };
  1347.  
  1348. int CMDNet( cmdP, cntL, eFP )    /* load network data */
  1349. CMD        *cmdP;        /* pointer to command table */
  1350. long         cntL;        /* # of items converted */
  1351. FILE        *eFP;        /* error file pointer */
  1352. {
  1353.     FILE    *fp;        /* file pointer */
  1354.     int         cI, rI;    /* column, row indicies */
  1355.     float    *pF;        /* float pointer */
  1356.     long     dL;        /* work date */
  1357.     int         dxI;        /* date index */
  1358.     double     vD;        /* work double */
  1359.     char    *sp, *ep;    /* work string pointers */
  1360.     char     buf[1024];    /* max data buffer */
  1361.  
  1362.     dL = Date2BinL( NetDateCA );    /* convert network data date */
  1363.     for( rI = 0; rI < FVecI; rI++ )
  1364.     if ( dL == FDateLP[rI] ) break;
  1365.     if ( rI >= FVecI )
  1366.     return( MsgER( "Specified date is not in data-base" ) );
  1367.     dxI = rI;
  1368.  
  1369.     /* --- see that file is there --- */
  1370.  
  1371.     if ( (fp = fopen( NetFileCA, "r" )) == (FILE *)0 )
  1372.     return( MsgER( "Network data file does not exist" ) );
  1373.  
  1374.     /* --- check for memory & allocate if requried --- */
  1375.  
  1376.     if ( NetValFP[0] == (float *)0 ) {
  1377.     /* --- must allocate space (deallocated when DB loaded --- */
  1378.     for( cI = 0; cI < ASof(NetValFP); cI++ ) {
  1379.         NetValFP[cI] = (float *)malloc( FVecI*sizeof(float) );
  1380.         if ( NetValFP[cI] == (float *)0 ) 
  1381.         return(MsgER("Insufficient memory to allocate network area" ));
  1382.     }
  1383.     }
  1384.  
  1385.     /* --- Clear out memory --- */
  1386.  
  1387.     NetFieldsI = 0;
  1388.     for( cI = 0; cI < ASof(NetValFP); cI++ ) {
  1389.     pF = NetValFP[rI];
  1390.     for( cI = 0; cI < FVecI; cI++, pF++ ) *pF = 0.0;
  1391.     }
  1392.     AnlFldXI = 0;            /* start with first column */
  1393.     strcpy( AnlDateCA, NetDateCA );    /* use network date */
  1394.  
  1395.     /* --- Read file into memory --- */
  1396.  
  1397.     rI = dxI;
  1398.     while( (sp = fgets( buf, sizeof(buf), fp )) != (char *)0 ) {
  1399.     while( *sp && *sp <= ' ' ) sp++;    /* skip leading spaces */
  1400.     if ( *sp == 0 || *sp == '!' ) continue;
  1401.     for( cI = 0; cI < ASof(NetValFP); cI++ ) {
  1402.         while( *sp && *sp <= ' ' ) sp++;    /* skip leading spaces */
  1403.         if ( *sp == 0 || *sp == '!' ) break;
  1404.         vD = strtod( sp, &ep );        /* try conversion */
  1405.         if ( sp == ep ) break;        /* error */
  1406.         NetValFP[cI][rI] = vD;        /* store data */
  1407.         sp = ep;
  1408.     }
  1409.     if ( cI > NetFieldsI ) NetFieldsI = cI;
  1410.     rI++;
  1411.     if ( rI >= FVecI ) break;
  1412.     }
  1413.     fclose( fp );
  1414.  
  1415.     return( 0 );
  1416. }
  1417.  
  1418. int CMDPrice( cmdP, cntL, eFP )    /* Check that price field is valid */
  1419. CMD        *cmdP;        /* pointer to command table */
  1420. long         cntL;        /* # of items converted */
  1421. FILE        *eFP;        /* error file pointer */
  1422. {
  1423.     FDES     PriceFD;    /* field descriptor */
  1424.  
  1425.     if ( FieldParseI( &PriceFD, PrFldCP ) != 0 ) {
  1426.     MsgER( "Invalid price field" );
  1427.     PriceErr:
  1428.     PriceFP = (float *)0;
  1429.     strcpy( PrFldCP, "" );
  1430.     return( -1 );
  1431.     }
  1432.     if ( (PriceFD.FFlagI & FD_FOUND) == 0 ) {
  1433.     MsgER( "Price field was not found in data-base" );
  1434.     goto PriceErr;
  1435.     }
  1436.  
  1437.     PriceFP = PriceFD.FFieldTP->FDataFP;
  1438.     return( 0 );
  1439. }
  1440.  
  1441. INPUT LISTAnal[] = {
  1442.     ISTR( AnlDateCA, IO_DTE|IO_OPT ),
  1443.     IVAL( AnlFldXI,  IO_INT|IO_OPT, FMT_INTU, 0 ),
  1444.     INUL
  1445. };
  1446.  
  1447. int CMDAnal( cmdP, cntL, eFP )    /* perform analysis of data */
  1448. CMD        *cmdP;        /* pointer to command table */
  1449. long         cntL;        /* # of items converted */
  1450. FILE        *eFP;        /* error file pointer */
  1451. {
  1452.     long     dL;        /* date to start analysis at */
  1453.     int         fldXI;        /* column to analyze */
  1454.     int         rI;        /* row to start at */
  1455.     int         nI;        /* # of items to analyze */
  1456.     int         rcI=0;        /* return code */
  1457.     int        *wBSSigIP=0;    /* buy/sell signal array */
  1458.     float    *wEqFP=0;    /* equity */
  1459.     float    *wPLFP=0;    /* P&L */
  1460.  
  1461.     float    *NSigFP;    /* neural network signal */
  1462.  
  1463.     if ( FieldsI == 0 )
  1464.     return( MsgER( "Must load data-base prior to analysis" ) );
  1465.     if ( PriceFP == (float *)0 )
  1466.     return( MsgER( "Must specify price field prior to analysis" ) );
  1467.     if ( NetFieldsI == 0 )
  1468.     return( MsgER( "Must load network data prior to analysis" ) );
  1469.  
  1470.     fldXI = (cntL>1? AnlFldXI:0);
  1471.     if ( fldXI >= NetFieldsI )
  1472.     return( MsgER( "Requested column is not in network data\n" ) );
  1473.  
  1474.     dL = Date2BinL( AnlDateCA );
  1475.     for( rI = 0; rI < FVecI; rI++ )
  1476.     if ( dL <= FDateLP[rI] ) break;
  1477.     if ( rI >= FVecI )
  1478.     return( MsgER( "Requested date is past end of data-base" ) );
  1479.  
  1480.     /* --- allocate temporary storage --- */
  1481.  
  1482.     nI = FVecI - rI;
  1483.     wBSSigIP = (int *)  malloc( nI * sizeof(int) );
  1484.     wEqFP    = (float *)malloc( nI * sizeof(float) );
  1485.     wPLFP    = (float *)malloc( nI * sizeof(float) );
  1486.     if ( wBSSigIP == (int *)0 || wEqFP == (float *)0 || wPLFP == (float *)0 ) {
  1487.     MsgER( "Could not allocate temporary memory required for analysis" );
  1488.     rcI = -1;
  1489.     goto Done;
  1490.     }
  1491.  
  1492.     NetEvalV( NetValFP[fldXI]+rI, PriceFP+rI, FDateLP+rI,
  1493.     wBSSigIP, wEqFP, wPLFP,
  1494.     nI, (ReportFI&RPT_TRANS)?1:0, eFP,
  1495.     ShortLowD, ShortHighD, LongLowD, LongHighD, CapitalD );
  1496.  
  1497.     if ( LogFP != (FILE *)0 )
  1498.     NetEvalV( NetValFP[fldXI]+rI, PriceFP+rI, FDateLP+rI,
  1499.         wBSSigIP, wEqFP, wPLFP,
  1500.         nI, (ReportFI&RPT_TRANS)?1:0, LogFP,
  1501.         ShortLowD, ShortHighD, LongLowD, LongHighD, CapitalD );
  1502.  
  1503. Done:
  1504.     if ( wBSSigIP == (int *)0   ) free( (void *)wBSSigIP );
  1505.     if ( wEqFP    == (float *)0 ) free( (void *)wEqFP    );
  1506.     if ( wPLFP    == (float *)0 ) free( (void *)wPLFP    );
  1507.     return( rcI );
  1508. }
  1509.  
  1510. /************************************************************************
  1511.  *                                    *
  1512.  *        Non-Linear Dynamical Systems                *
  1513.  *                                    *
  1514.  ************************************************************************
  1515.  */
  1516. #ifdef NLDS
  1517. INPUT LISTSeries[] = {        /* series,start,end */
  1518.     ISTR( NFieldCA,   IO_FLD ),
  1519.     ISTR( NStrDateCA, IO_DTE|IO_OPT ),
  1520.     ISTR( NEndDateCA, IO_DTE|IO_OPT ),
  1521.     INUL
  1522. };
  1523.  
  1524. LIT LITDerivative[] = {
  1525.   { "no",    0 },
  1526.   { "yes",    1 },
  1527.   { 0 }
  1528. };
  1529.  
  1530. LIT LITPrintType[] = {
  1531.   { "formatted",    PRT_TEXT },
  1532.   { "lotus",        PRT_COMMA },
  1533.   { "excel",        PRT_TAB },
  1534.   { 0 }
  1535. };
  1536.  
  1537. INPUT LISTPrinter[] = {        /* filename,type */
  1538.     ISTR( PrintFileCA,  IO_FNM ),
  1539.     IVAL( PrintTypeI, IO_INT|IO_LITONLY,FMT_INTS,LITPrintType ),
  1540.     INUL
  1541. };
  1542.  
  1543. INPUT LISTGo[] = {        /* startdim,enddim */
  1544.     IVAL( StartDimI, IO_INT|IO_OPT,FMT_INTU,0 ),
  1545.     IVAL( EndDimI,   IO_INT|IO_OPT,FMT_INTU,0 ),
  1546.     INUL
  1547. };
  1548.  
  1549. /* --- NLDSGoI() --- Start up procedure for NLDS
  1550.  */
  1551. int NLDSGoI( fp, WhatStrSP, cmTP, PromptSP )
  1552. FILE        *fp;        /* where to put output */
  1553. char        *WhatStrSP;    /* what it is we are doing */
  1554. CMD        *cmTP;        /* command table pointer */
  1555. char        *PromptSP;    /* prompt string */
  1556. {
  1557.     if ( FieldsI <= 0 ) return( MustLoadDBI() );
  1558.  
  1559.     fprintf( fp,
  1560.     "* Entering Non-Linear Dynamical Systems Analysis by %s *\n",
  1561.     WhatStrSP );
  1562.     DoCommandsI( cmTP, PromptSP );
  1563.     fprintf( fp,
  1564.     "* Leaving Non-Linear Dynamical Systems Analysis by %s *\n",
  1565.     WhatStrSP );
  1566.     return( 0 );
  1567. }
  1568.  
  1569. /* --- CMDCkSeries() ---- Check series for valid syntax
  1570.  *    field.label[,date][,date]
  1571.  *        Field should have no offset assoicated with it
  1572.  *        dates are optional.
  1573.  *        NFldDataFP points to vector
  1574.  *        NFldStrXI is the offset into the vector to start at
  1575.  *        NFldLengthI is the number of items to use.
  1576.  */
  1577.  
  1578. int CMDCkSeries( cmdP, cntL, eFP )    /* Check for valid series & dates */
  1579. CMD        *cmdP;        /* pointer to command table */
  1580. long         cntL;        /* # of items converted */
  1581. FILE        *eFP;        /* error file pointer */
  1582. {   int         rcI = 0;    /* return code from parsing */
  1583.     long     dL;        /* work long */
  1584.     int         wxI;        /* work index */
  1585.  
  1586.     NFieldFlagI = 0;            /* not valid yet */
  1587.     if ( FieldParseI( &NFieldT, NFieldCA ) )
  1588.     return( MsgER( "Could not parse field" ) );
  1589.     if ( (NFieldT.FFlagI & FD_FOUND) == 0 )
  1590.     return( MsgER( "Field specified could not be found" ) );
  1591.     if ( NFieldT.FOffI != 0 )
  1592.     return( MsgER( "Offset is not permitted.  Use date range instead" ) );
  1593.  
  1594.     NFldDataFP  = NFieldT.FFieldTP->FDataFP;    /* point to the data */
  1595.  
  1596.     NStrDateL = Date2BinL( NStrDateCA );
  1597.     NEndDateL = Date2BinL( NEndDateCA );
  1598.     if ( NStrDateL > NEndDateL )
  1599.     { dL = NStrDateL; NStrDateL = NEndDateL; NEndDateL = dL; }
  1600.  
  1601.     NFldStrXI = -1;
  1602.     for( wxI = 0; wxI < FVecI; wxI++ ) {
  1603.     if ( NFldStrXI < 0 && FDateLP[wxI] >= NStrDateL )
  1604.         NFldStrXI = wxI;        /* start index */
  1605.     if ( FDateLP[wxI] > NEndDateL ) break;
  1606.     }
  1607.     NFldLengthI = wxI - NFldStrXI;
  1608.     if ( NFldStrXI < 0 || NFldLengthI <= 10 )
  1609.     return( MsgER( "Too few items for analysis" ) );
  1610.  
  1611. Done:
  1612.     if ( rcI == 0 ) NFieldFlagI = 1;
  1613.     return( rcI );        /* return code */
  1614. }
  1615.  
  1616. /* --- CMDPrinter() --- Copy printer file name to type file name
  1617.  */
  1618. int CMDPrinter( cmdP, cntL, eFP )    /* copy printer file to type file */
  1619. CMD        *cmdP;        /* pointer to command table */
  1620. long         cntL;        /* # of items converted */
  1621. FILE        *eFP;        /* error file pointer */
  1622. {   FILE    *fp;        /* new output file */
  1623.     char    *OpenModeSP;    /* open mode */
  1624.  
  1625.     OpenModeSP = PrintTypeI==PRT_TEXT? "a":"w";
  1626.     if ( (fp = fopen( PrintFileCA, OpenModeSP )) == (FILE *)0 )
  1627.     return( MsgER( "Could not open printer output file" ) );
  1628.  
  1629.     if ( PrintFileFP ) fclose( PrintFileFP );
  1630.     if ( PrintTypeI == PRT_TEXT ) {
  1631.     fputs( "\n\f", fp );
  1632.     fflush( fp );
  1633.     }
  1634.     PrintFileFP = fp;
  1635.     PrintFilePosL = ftell( fp );
  1636.     strcpy( TypeFileCA, PrintFileCA );
  1637.  
  1638.     return( 0 );
  1639. }
  1640.  
  1641. int CheckPointPrinter()        /* check-point printer output */
  1642. {
  1643.     if ( PrintFileFP ) {
  1644.     fflush( PrintFileFP );
  1645.     fclose( PrintFileFP );
  1646.     PrintFileFP = fopen( PrintFileCA, "a" );
  1647.     return( PrintFileFP? 1:(-1) );
  1648.     }
  1649.     return( 0 );
  1650. }
  1651.  
  1652. int CMDTypeFile( cmdP, cntL, eFP )    /* copy printer file to type file */
  1653. CMD        *cmdP;        /* pointer to command table */
  1654. long         cntL;        /* # of items converted */
  1655. FILE        *eFP;        /* error file pointer */
  1656. {   FILE    *fp;        /* input file */
  1657.     int         c;        /* work char */
  1658.     int         scrcntI;    /* screen count */
  1659.  
  1660.     CheckPointPrinter();
  1661.     if ( (fp = fopen( TypeFileCA, "r" )) == (FILE *)0 )
  1662.     return( MsgER( "Could not open input file to type" ) );
  1663.     if ( strcmp( TypeFileCA, PrintFileCA ) == 0 )
  1664.     fseek( fp, PrintFilePosL, 0 );        /* skip to new stuff */
  1665.  
  1666.     fprintf( stdout, "-- Displaying file <%s> --\n", TypeFileCA );
  1667.     for( c=0, scrcntI = 21;;) {
  1668.     c = fgetc( fp );
  1669.     if ( feof(fp) || ferror(fp) ) break;
  1670.     fputc( c, stdout );
  1671.     if ( c == '\n' ) {
  1672.         if ( --scrcntI <= 0 ) {
  1673.         scrcntI = 22;        /* pause at end of screen */
  1674.         fputs( "\rPress Space to Continue", stdout );
  1675.         fflush( stdout );
  1676.         while( getch() != ' ' )
  1677.             ;
  1678.         fputs( "\r                        \r", stdout );
  1679.         }
  1680.     }
  1681.     }
  1682.     if ( c != '\n' ) fputc( '\n', stdout );
  1683.     fflush( stdout );
  1684.     fclose( fp );
  1685.     return( 0 );
  1686. }
  1687.  
  1688. int CMDNQuit( cmdP, cntL, eFP )    /* Quit NLDS section */
  1689. CMD        *cmdP;        /* pointer to command table */
  1690. long         cntL;        /* # of items converted */
  1691. FILE        *eFP;        /* error file pointer */
  1692. {
  1693.     if ( PrintFileFP != (FILE *)0 ) fclose( PrintFileFP );
  1694.     PrintFileFP = (FILE *)0;
  1695.     QuitI = 1;
  1696.     return( 0 );
  1697. }
  1698.  
  1699. int CMDGoHurst( cmdP, cntL, eFP )    /* Hurst Coefficient Analysis */
  1700. CMD        *cmdP;        /* pointer to command table */
  1701. long         cntL;        /* # of items converted */
  1702. FILE        *eFP;        /* error file pointer */
  1703. {   unsigned     vlenU;        /* length of each vector */
  1704.     int         rcI=0;        /* return code */
  1705.     float    *hurstFP, *nFP, *rsFP, *lognFP, *logrsFP;
  1706.     FILE    *fp;        /* output file pointer */
  1707.     char    *fmtSP;        /* format for printfs */
  1708.     char    *hdrSP;        /* format for header */
  1709.     int         wxI;        /* work index */
  1710.     int         PrtMdeI;    /* local print mode */
  1711.  
  1712.     if ( !NFieldFlagI )
  1713.     return( MsgER( "Must specify series to analyze" ) );
  1714.  
  1715.     if ( IterationsI < 5 ) IterationsI = 5;
  1716.     vlenU = ((unsigned)IterationsI+2)*sizeof(*hurstFP);
  1717.     hurstFP = (float *)malloc( 5*vlenU );
  1718.     nFP     = hurstFP    + IterationsI;
  1719.     rsFP    = nFP    + IterationsI;
  1720.     lognFP  = rsFP    + IterationsI;
  1721.     logrsFP = lognFP    + IterationsI;
  1722.  
  1723.     HurstI( hurstFP, nFP, rsFP, lognFP, logrsFP,
  1724.     &NFldDataFP[NFldStrXI], NFldLengthI,
  1725.     StartWinI, DeltaWinI, IterationsI, DerivativeI );
  1726.  
  1727.     if ( PrintFileFP ) {
  1728.     fp = PrintFileFP;
  1729.     PrtMdeI = PrintTypeI;
  1730.     } else {
  1731.     fp = stdout;
  1732.     PrtMdeI = PRT_TEXT;
  1733.     }
  1734.     switch( PrtMdeI ) {
  1735.     case PRT_TEXT:    default:
  1736.     hdrSP = " %7s %7s %8s %7s %7s\n";
  1737.     fmtSP = " %7.3f %7.0f %8.3f %7.3f %7.3f\n";
  1738.     break;
  1739.     case PRT_COMMA:
  1740.     hdrSP = "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n";
  1741.     fmtSP = "%.3f,%.0f,%.3f,%.3f,%.3f\n";
  1742.     break;
  1743.     case PRT_TAB:
  1744.     hdrSP = "%s\t%s\t%s\t%s\t%s\n";
  1745.     fmtSP = "%.3f\t%.0f\t%.3f\t%.3f\t%.3f\n";
  1746.     break;
  1747.     }
  1748.  
  1749.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  1750.     fprintf( fp, hdrSP, "hurst", "n", "rs", "log(n)", "log(rs)" );
  1751.     for( wxI = 0; wxI < IterationsI; wxI++ )
  1752.     fprintf( fp, fmtSP,
  1753.         hurstFP[wxI], nFP[wxI], rsFP[wxI],
  1754.         lognFP[wxI], logrsFP[wxI] );
  1755.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  1756.     CheckPointPrinter();
  1757.  
  1758. Done:
  1759.     if ( hurstFP ) free( (void *)hurstFP );
  1760.     return( rcI );
  1761. }
  1762.  
  1763. CMD cmdlHURST[] = {
  1764.   { "series",        "Data series to analyze",
  1765.             CMDCkSeries, ILIST( LISTSeries, IO_DSP ) },
  1766.   { "startwindowsize",    "Initial window size",
  1767.             0, IVAL( StartWinI, IO_INT,FMT_INTU, 0 ) },
  1768.   { "deltawindowsize",    "Change in window size",
  1769.             0, IVAL( DeltaWinI, IO_INT,FMT_INTU, 0 ) },
  1770.   { "iterations",    "Number of iterations to perform",
  1771.             0, IVAL( IterationsI, IO_INT,FMT_INTU, 0 ) },
  1772.   { "derivative",    "Use derivative of series",
  1773.     0, IVAL( DerivativeI, IO_INT|IO_LITONLY,FMT_INTS,LITDerivative ) },
  1774.   { "dictionary",    "Display dictionary",
  1775.             CMDDict,   IACTR0 },
  1776.   { "printfile",    "Printed output data file",
  1777.             CMDPrinter, ILIST( LISTPrinter, IO_DSP ) },
  1778.   { "type",        "Type a file to console",
  1779.             CMDTypeFile, ISTR( TypeFileCA, IO_FNM|IO_OPT ) },
  1780.   { "go",        "Hurst coefficient analysis",
  1781.             CMDGoHurst, IACTR0 },
  1782.   { "quit",        "Return to main menu",
  1783.             CMDNQuit,   IACTR0 },
  1784.   { 0 }
  1785. };
  1786.  
  1787. int CMDHurst( cmdP, cntL, eFP )    /* enter Hurst Coefficient section */
  1788. CMD        *cmdP;        /* pointer to command table */
  1789. long         cntL;        /* # of items converted */
  1790. FILE        *eFP;        /* error file pointer */
  1791. {   return( NLDSGoI( eFP, "Hurst Exponent", &cmdlHURST[0], "Hurst" ) );
  1792. }
  1793.  
  1794. int CMDGoCorDim( cmdP, cntL, eFP )    /* compute correlation dimension */
  1795. CMD        *cmdP;        /* pointer to command table */
  1796. long         cntL;        /* # of items converted */
  1797. FILE        *eFP;        /* error file pointer */
  1798. {   unsigned     vlenU;        /* length of each vector */
  1799.     int         rcI=0;        /* return code */
  1800.     float    *rFP, *crFP, *logrFP, *logcrFP, *regFP;
  1801.     FILE    *fp;        /* output file pointer */
  1802.     char    *fmtSP;        /* format for printfs */
  1803.     char    *hdrSP;        /* format for header */
  1804.     char    *sp;        /* work string pointer */
  1805.     int         wxI;        /* work index */
  1806.     int         DimI;        /* current dimension */
  1807.     int         PrtMdeI;    /* printer mode */
  1808.  
  1809.     if ( !NFieldFlagI )
  1810.     return( MsgER( "Must specify series to analyze" ) );
  1811.  
  1812.     if ( cntL < 2 ) {
  1813.     switch( cntL ) {
  1814.     case 0:    StartDimI = EndDimI = EvolveI;    break;
  1815.     case 1: EndDimI = EvolveI;        break;
  1816.     }
  1817.     }
  1818.     if ( StartDimI > EndDimI )
  1819.     { DimI = StartDimI; StartDimI = EndDimI; EndDimI = DimI; }
  1820.  
  1821.     if ( IterationsI < 10 ) IterationsI = 10;
  1822.     vlenU = ((unsigned)IterationsI+2)*sizeof(float);
  1823.     rFP     = (float *)malloc( 5*vlenU );
  1824.     if ( rFP == (float *)0 ) {
  1825.     rcI = -1;
  1826.     goto Done;
  1827.     }
  1828.     crFP    = rFP    + IterationsI;
  1829.     logrFP  = crFP    + IterationsI;
  1830.     logcrFP = logrFP    + IterationsI;
  1831.     regFP   = logcrFP    + IterationsI;
  1832.  
  1833.     if ( PrintFileFP ) {
  1834.     fp = PrintFileFP;
  1835.     PrtMdeI = PrintTypeI;
  1836.     } else {
  1837.     fp = stdout;
  1838.     PrtMdeI = PRT_TEXT;
  1839.     }
  1840.     for( DimI = StartDimI; DimI <= EndDimI; DimI++ ) {
  1841.  
  1842.     sp = (PrtMdeI == PRT_COMMA? "\"":"");
  1843.     fprintf( fp,
  1844.       "%sCorrDim: Embedding Dim=%d for <%s> from <%s> to <%s>\n%s",
  1845.        sp,DimI,NFieldCA,NStrDateCA,NEndDateCA,sp );
  1846.     CheckPointPrinter();
  1847.     if ( PrintFileFP )
  1848.         fprintf( stderr,
  1849.         "Embedding Dim=%d for <%s> from <%s> to <%s>\n",
  1850.         DimI,NFieldCA,NStrDateCA,NEndDateCA );
  1851.  
  1852. #ifdef CORRDIMORG
  1853.     CorrDimI( rFP, crFP, logrFP, logcrFP, regFP,
  1854.         &NFldDataFP[NFldStrXI], NFldLengthI,
  1855.         DimI, tauI, dtD, radiusD, IterationsI );
  1856. #else
  1857.     CorrDim2I( rFP, crFP, logrFP, logcrFP, regFP,
  1858.         &NFldDataFP[NFldStrXI], NFldLengthI,
  1859.         DimI, tauI, IterationsI );
  1860. #endif
  1861.  
  1862.     switch( PrtMdeI ) {
  1863.     case PRT_TEXT:    default:
  1864.         hdrSP = " %7s %7s %8s %7s %7s\n";
  1865.         fmtSP = " %7.3f %7.3f %8.3f %7.3f %7.3f\n";
  1866.         break;
  1867.     case PRT_COMMA:
  1868.         hdrSP = "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n";
  1869.         fmtSP = "%.3f,%.3f,%.3f,%.3f,%.3f\n";
  1870.         break;
  1871.     case PRT_TAB:
  1872.         hdrSP = "%s\t%s\t%s\t%s\t%s\n";
  1873.         fmtSP = "%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n";
  1874.         break;
  1875.     }
  1876.  
  1877.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  1878.     fprintf( fp, hdrSP, "reg cor", "r", "cd", "log(r)", "log(cd)" );
  1879.     for( wxI = 0; wxI < IterationsI; wxI++ )
  1880.         fprintf( fp, fmtSP,
  1881.         regFP[wxI], rFP[wxI], crFP[wxI],
  1882.         logrFP[wxI], logcrFP[wxI] );
  1883.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  1884.     CheckPointPrinter();
  1885.     }
  1886. Done:
  1887.     if ( rFP ) free( (void *)rFP );
  1888.     return( rcI );
  1889. }
  1890.  
  1891. CMD cmdlCORDIM[] = {
  1892.   { "series",        "Data series to analyze",
  1893.             CMDCkSeries, ILIST( LISTSeries, IO_DSP ) },
  1894.   { "embedding",    "Embedding dimension",
  1895.             0, IVAL( EmbeddingI, IO_INT,FMT_INTU,0 ) },
  1896.   { "tau",        "Lag for re-creating phase space",
  1897.             0, IVAL( tauI, IO_INT,FMT_INTU,0 ) },
  1898. #ifdef CORRDIMORG
  1899.   { "radius",        "initial radius",
  1900.             0, IVAL( radiusD, IO_FLT,FMT_FLT3, 0 ) },
  1901.   { "deltaradius",    "change in radius",
  1902.             0, IVAL( dtD, IO_FLT,FMT_FLT3, 0 ) },
  1903. #endif
  1904.   { "iterations",    "Number of iterations to perform",
  1905.             0, IVAL( IterationsI, IO_INT,FMT_INTU, 0 ) },
  1906.   { "dictionary",    "Display dictionary",
  1907.             CMDDict,   IACTR0 },
  1908.   { "printfile",    "Printed output data file",
  1909.             CMDPrinter, ILIST( LISTPrinter, IO_DSP ) },
  1910.   { "type",        "Type a file to console",
  1911.             CMDTypeFile, ISTR( TypeFileCA, IO_FNM|IO_OPT ) },
  1912.   { "go",        "Correlation Dimension analysis",
  1913.             CMDGoCorDim, ILIST( LISTGo, IO_DSP ) },
  1914.   { "quit",        "Return to main menu",
  1915.             CMDNQuit,   IACTR0 },
  1916.   { 0 }
  1917. };
  1918.  
  1919. int CMDCorDim( cmdP, cntL, eFP ) /* enter Correlation Dimension section */
  1920. CMD        *cmdP;        /* pointer to command table */
  1921. long         cntL;        /* # of items converted */
  1922. FILE        *eFP;        /* error file pointer */
  1923. {   return( NLDSGoI( eFP, "Correlation Dimension", &cmdlCORDIM[0],"CorrDim" ) );
  1924. }
  1925.  
  1926. int CMDGoLyapunov( cmdP, cntL, eFP )    /* compute lyapunov exponent */
  1927. CMD        *cmdP;        /* pointer to command table */
  1928. long         cntL;        /* # of items converted */
  1929. FILE        *eFP;        /* error file pointer */
  1930. {   unsigned     vlenU;        /* length of each vector */
  1931.     int         rcI=0;        /* return code */
  1932.     float    *zlyapFP, *EvolveItFP, *diFP, *DFFP, *regFP;
  1933.     FILE    *fp;        /* output file pointer */
  1934.     char    *fmtSP;        /* format for printfs */
  1935.     char    *hdrSP;        /* format for header */
  1936.     char    *sp;        /* work char pointer */
  1937.     int         wxI;        /* work index */
  1938.     int         DimI;        /* current dimension */
  1939.     int         PrtMdeI;    /* local print mode */
  1940.     int         ItersI;    /* # of actual interations */
  1941.  
  1942.     if ( !NFieldFlagI )
  1943.     return( MsgER( "Must specify series to analyze" ) );
  1944.  
  1945.     if ( cntL < 2 ) {
  1946.     switch( cntL ) {
  1947.     case 0:    StartDimI = EndDimI = EmbeddingI;    break;
  1948.     case 1: EndDimI = EmbeddingI;            break;
  1949.     }
  1950.     }
  1951.     if ( StartDimI > EndDimI )
  1952.     { DimI = StartDimI; StartDimI = EndDimI; EndDimI = DimI; }
  1953.  
  1954.     vlenU = ((unsigned)NFldLengthI+2)*sizeof(float);
  1955.     zlyapFP    = (float *)malloc( 4*vlenU );
  1956.     if ( zlyapFP == (float *)0 ) {
  1957.     rcI = -1;
  1958.     goto Done;
  1959.     }
  1960.     EvolveItFP    = zlyapFP    + NFldLengthI;
  1961.     diFP    = EvolveItFP    + NFldLengthI;
  1962.     DFFP    = diFP        + NFldLengthI;
  1963.  
  1964.     if ( PrintFileFP ) {
  1965.     fp = PrintFileFP;
  1966.     PrtMdeI = PrintTypeI;
  1967.     } else {
  1968.     fp = stdout;
  1969.     PrtMdeI = PRT_TEXT;
  1970.     }
  1971.     for( DimI = StartDimI; DimI <= EndDimI; DimI++ ) {
  1972.  
  1973.     sp = (PrtMdeI == PRT_COMMA? "\"":"");
  1974.     fprintf( fp,
  1975.       "%sLyapunov: Embedding Dim=%d for <%s> from <%s> to <%s>\n%s",
  1976.       sp,DimI,NFieldCA,NStrDateCA,NEndDateCA,sp );
  1977.     CheckPointPrinter();
  1978.     if ( PrintFileFP )
  1979.         fprintf( stderr,
  1980.         "Embedding Dim=%d for <%s> from <%s> to <%s>\n",
  1981.         DimI,NFieldCA,NStrDateCA,NEndDateCA );
  1982.  
  1983.     ItersI = LyapunovI( zlyapFP, EvolveItFP, diFP, DFFP,
  1984.         &NFldDataFP[NFldStrXI], NFldLengthI,
  1985.         DimI, tauI, dtD, NMaxScaleD, NMinScaleD,
  1986.         EvolveI, LagMinimumI );
  1987.  
  1988.     switch( PrintTypeI ) {
  1989.     case PRT_TEXT:    default:
  1990.         hdrSP = " %7s %8s %7s %7s\n";
  1991.         fmtSP = " %7.3f %8.0f %7.3f %7.3f\n";
  1992.         break;
  1993.     case PRT_COMMA:
  1994.         hdrSP = "\"%s\",\"%s\",\"%s\",\"%s\"\n";
  1995.         fmtSP = "%.3f,%.0f,%.3f,%.3f\n";
  1996.         break;
  1997.     case PRT_TAB:
  1998.         hdrSP = "%s\t%s\t%s\t%s\n";
  1999.         fmtSP = "%.3f\t%.0f\t%.3f\t%.3f\n";
  2000.         break;
  2001.     }
  2002.  
  2003.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  2004.     fprintf( fp, hdrSP, "zlyap", "Evolve", "di", "DF" );
  2005.     for( wxI = 0; wxI < ItersI; wxI++ )
  2006.         fprintf( fp, fmtSP,
  2007.         zlyapFP[wxI], EvolveItFP[wxI],
  2008.         diFP[wxI], DFFP[wxI] );
  2009.     if ( PrintTypeI == PRT_TEXT ) fprintf( fp, "\n" );
  2010.     CheckPointPrinter();
  2011.     }
  2012. Done:
  2013.     if ( zlyapFP ) free( (void *)zlyapFP );
  2014.     return( rcI );
  2015. }
  2016.  
  2017. CMD cmdlLYAP[] = {
  2018.   { "series",        "Data series to analyze",
  2019.             CMDCkSeries, ILIST( LISTSeries, IO_DSP ) },
  2020.   { "embedding",    "Embedding dimension",
  2021.             0, IVAL( EmbeddingI, IO_INT,FMT_INTU,0 ) },
  2022.   { "tau",        "Lag for re-creating phase space",
  2023.             0, IVAL( tauI, IO_INT,FMT_INTU,0 ) },
  2024.   { "minscale",        "Minimum radius",
  2025.             0, IVAL( NMinScaleD, IO_FLT,FMT_FLT3,0 ) },
  2026.   { "maxscale",        "Maximum radius",
  2027.             0, IVAL( NMaxScaleD, IO_FLT,FMT_FLT3,0 ) },
  2028.   { "evolve",        "Number of time steps per evolution",
  2029.             0, IVAL( EvolveI, IO_INT,FMT_INTU,0 ) },
  2030.   { "lagminimum",    "Minimum lag between points",
  2031.             0, IVAL( LagMinimumI, IO_INT,FMT_INTU,0 ) },
  2032.   { "dictionary",    "Display dictionary",
  2033.             CMDDict,   IACTR0 },
  2034.   { "printfile",    "Printed output data file",
  2035.             CMDPrinter, ILIST( LISTPrinter, IO_DSP ) },
  2036.   { "type",        "Type a file to console",
  2037.             CMDTypeFile, ISTR( TypeFileCA, IO_FNM|IO_OPT ) },
  2038.   { "go",        "Lyapunov Exponent analysis",
  2039.             CMDGoLyapunov, ILIST( LISTGo, IO_DSP ) },
  2040.   { "quit",        "Return to main menu",
  2041.             CMDNQuit,   IACTR0 },
  2042.   { 0 }
  2043. };
  2044.  
  2045. int CMDLyapunov( cmdP, cntL, eFP ) /* enter Lyapunov Exponent section */
  2046. CMD        *cmdP;        /* pointer to command table */
  2047. long         cntL;        /* # of items converted */
  2048. FILE        *eFP;        /* error file pointer */
  2049. {   return( NLDSGoI( eFP, "Lyapunov Exponent", &cmdlLYAP[0], "Lyapunov" ));
  2050. }
  2051. #endif
  2052.  
  2053. /* --- Main Command Interpreter Table --- */
  2054.  
  2055. CMD ct[] = {
  2056.   { "load",        "Load data-base to memory",
  2057.             CMDLoad,   ILIST( LISTLoad, IO_DSP ) },
  2058.   { "interpolate",     "Interpolate zero values",
  2059.             CMDInterp, IACTR0 },
  2060.   { "scale",        "Scale data to min,max range",
  2061.             CMDScale,  ILIST( LISTScale, IO_DSP ) },
  2062.  
  2063.   { "transform",    "Write out transforms",
  2064.             CMDTrans,  ILIST( LISTTrans, IO_DSP ) },
  2065.   { "saveall",        "Save data-base",
  2066.             CMDSave,   ILIST( LISTSave,  IO_DSP ) },
  2067.   { "log",        "Log actions to a file",
  2068.             CMDLog,       ISTR(  LogFile, IO_DSP|IO_FNM ) },
  2069.   { "dictionary",    "Display dictionary",
  2070.             CMDDict,   IACTR0 },
  2071.   { "quit",        "Terminate this program",
  2072.             CMDQuit,   IACTR0 },
  2073.  
  2074.   /* --- Transformation commands --- */
  2075.  
  2076.   { "movingaverage",    "Moving average",
  2077.             CMDMovAvg, ILIST( LISTFldDay, IO_DSP ) },
  2078.   { "rsi",        "Relative strength indicator",
  2079.             CMDRSI,    ILIST( LISTFldDay, IO_DSP ) },
  2080.   { "stochastic",    "Stochastic with smoothing",
  2081.             CMDStoch,  ILIST( LISTStoch,  IO_DSP ) },
  2082.   { "volatility",    "Volatility",
  2083.             CMDVol,    ILIST( LISTFldDay, IO_DSP ) },
  2084.   { "highlow",        "Volatility using high,low series",
  2085.             CMDHLVol,  ILIST( LISTFFDay,  IO_DSP ) },
  2086.   { "slope",        "Slope between two series",
  2087.             CMDSlope,  ILIST( LISTOFFDay, IO_DSP ) },
  2088.   { "intercept",    "Intercept betweem two seroes",
  2089.             CMDInter,  ILIST( LISTOFFDay, IO_DSP ) },
  2090.   { "rollingcorrelation","Rolling correlation",
  2091.             CMDCorr,   ILIST( LISTFFDay,  IO_DSP ) },
  2092.   { "raw",        "Copy a column of data",
  2093.             CMDRaw,    ISTR( XFieldCA, IO_FLD|IO_DSP ) },
  2094.  
  2095.   /* --- Analysis Section --- */
  2096.  
  2097.   { "network",        "Load the neural net output", 
  2098.             CMDNet, ILIST( LISTNet, IO_DSP ) },
  2099.   { "short",        "Net min,max for short position",
  2100.             0, ILIST( LISTShort, IO_DSP ) },
  2101.   { "long",        "Net min,max for long position",
  2102.             0, ILIST( LISTLong, IO_DSP ) },
  2103.   { "capital",        "Initial capital for analysis",
  2104.             0, IVAL( CapitalD, IO_FLT,FMT_FLT0, 0 ) },
  2105.   { "price",        "Field for buy/sell price",
  2106.             CMDPrice, ISTR( PrFldCP, IO_DSP|IO_FLD ) },
  2107.   { "report",        "Identify options for report", 0,
  2108.         IVAL( ReportFI, IO_INT|IO_LITONLY,FMT_INTS,LITReport ) },
  2109.   { "analyze",        "Analyze network data",
  2110.             CMDAnal, ILIST( LISTAnal, IO_DSP ) },
  2111.  
  2112.   /* --- Non-Linear Dynamical Systems --- */
  2113.  
  2114. #ifdef NLDS
  2115.   { "hurst",        "NLDS: Hurst exponent",
  2116.             CMDHurst, IACTR0 },
  2117.   { "correlationdime",    "NLDS: Correlation Dimension",
  2118.             CMDCorDim, IACTR0 },
  2119.   { "lyapunov",        "NLDS: Largest Lyapunov Exponent",
  2120.             CMDLyapunov, IACTR0 },
  2121. #endif
  2122.   { 0 }
  2123. };
  2124.  
  2125. int MsgER( buf )
  2126. char        *buf;
  2127. {
  2128.     fputs( buf, stderr );
  2129.     fputs( "\n", stderr );
  2130.     if ( LogFP != (FILE *)0 ) {
  2131.     fputs( buf, LogFP );
  2132.     fputs( "\n", LogFP );
  2133.     }
  2134.     return( -1 );
  2135. }
  2136.  
  2137. char *HelpSPP[] = {
  2138.   "Use: ?        Make a list of all current variable values\n",
  2139.   "     ??       List syntax of all variables AND commands\n",
  2140.   "     ?v       Print the current value of all variables staring with v\n",
  2141.   "     var?     Prints the syntax for this if unique, otherwise a list\n",
  2142.   "              of possible matches\n",
  2143.   "     command  executes a command.\n",
  2144.   "     var=val  assigns a value to a variable\n",
  2145.   "     var val  ditto\n",
  2146.   "\n",
  2147.   "     Commands and Variables may be abbreviated to the fewest characters\n",
  2148.   "     which provide for unique identification.\n",
  2149.   0
  2150. };
  2151.  
  2152.  
  2153. main(ac,av)
  2154. int         ac;        /* arg counter */
  2155. char        *av[];        /* arg pointer */
  2156. {   int         wxI;        /* work index */
  2157.  
  2158.     fputs(
  2159.     "Copyright (c) 1992, High-Tech Communications.  All rights reserved.\n",
  2160.     stdout );
  2161.     fputs(
  2162.     "103 Buckskin Court, Sewickley, PA 15143, USA.\n",
  2163.     stdout );
  2164.     fputs(
  2165.     "FAX: 412-741-6094  TEL: 412-741-7699\n", stdout );
  2166.  
  2167.     for( wxI = 0; HelpSPP[wxI] != (char *)0; wxI++ )
  2168.     fputs( HelpSPP[wxI], stdout );
  2169.     fflush( stdout );
  2170.  
  2171.     DoCommandsI( &ct[0], "Command" );
  2172.  
  2173.     exit( 0 );
  2174. }
  2175.